import java.util.Scanner; public class RowGame { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String no="Please enter the number of spaces for the game (1-40): "; int c=0; int player1; int player2; int spaces=promptNumberReadLine(scanner,no,40); int[] a=new int[spaces]; System.out.println(rowToString(a)); int win=0; while(win==0){ if(c%2==0){ player1=promptNumberReadLine(scanner,"X, please enter your move (1-"+spaces+"): ",spaces); while(makeMove(a,1,player1)==false){ System.out.println("That spaces is not available! Please try again."); System.out.println(rowToString(a)); player1=promptNumberReadLine(scanner,"X, please enter your move (1-"+spaces+"): ",spaces); } System.out.println(rowToString(a)); }else{ player2=promptNumberReadLine(scanner,"O, please enter your move (1-"+spaces+"): ",spaces); while(makeMove(a,2,player2)==false){ System.out.println("That spaces is not available! Please try again."); System.out.println(rowToString(a)); player2=promptNumberReadLine(scanner,"O, please enter your move (1-"+spaces+"): ",spaces); } System.out.println(rowToString(a)); } c++; win=winner(a); } if(win==1) System.out.println("Game over! X wins."); if(win==2) System.out.println("Game over! O wins."); if (win==-1) System.out.println("Game over! Tie game!"); } public static int promptNumberReadLine(Scanner s, String prompt, int max){ int num=1; String n=""; do { if(num < 1||num>max) System.out.println("That was not a valid number! Please try again."); System.out.print(prompt); while (!s.hasNextInt()) { s.nextLine(); System.out.println("That was not a valid number! Please try again."); System.out.print(prompt); } n= s.nextLine(); while(!isNumber(n)){ System.out.println("That was not a valid number! Please try again."); System.out.print(prompt); n= s.nextLine(); } int result = Integer.parseInt(n); num =result; } while (num < 1||num>max); return num; } public static boolean isNumber(String str) { for (int i = 0; i < str.length(); i++) if (Character.isDigit(str.charAt(i))==false) return Character.isDigit(str.charAt(i)); return str.length() > 0; } public static String rowToString(int[] a){ String row=""; for(int i=0;iplayer2) return 1; else if(player1a.length||position<1) return false; else{ if(a[position-1]==0){ for(int i=0;i=0)&&((position-b)<=(a.length-1))){ if((a[position-b]!=player)&&(a[position-b]!=0)&&(bbb==1)){ bb+=1; }else{ bbb=0; } if(a[position-b]==0) bbbb=0; b+=1; } if(bb>=1 && bbbb==1){ for(int i=(position-(bb+1)); i=0)&&((position+b)<=(a.length-1))){ if((a[position+b]!=player)&&(a[position+b]!=0)&&(bbb==1)){ bb+=1; }else{ bbb=0; } if(a[position+b]==0) bbbb=0; b+=1; } if(bb>=1 && bbbb==1){ for(int i=position; i=0)&&((position-b)<=(a.length-1))){ if((a[position-b]!=player)&&(a[position-b]!=0)&&(bbb==1)){ bb+=1; }else{ bbb=0; } if(a[position-b]==0) bbbb=0; b+=1; } if(bb>=1 && bbbb==1){ for(int i=(position-(bb+1)); i=0)&&((position+b)<=(a.length-1))){ if((a[position+b]!=player)&&(a[position+b]!=0)&&(bbb==1)){ bb+=1; }else{ bbb=0; } if(a[position+b]==0) bbbb=0; b+=1; } if(bb>=1 && bbbb==1){ for(int i=position; i