import java.util.*; public class Game { 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."); 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."); 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("a over! O wins."); if (win==-1) System.out.println("a over! Tie a!"); } 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){ a[position-1]=player; if(position==2){ if(a[0]!=player) if (a[0]!=0) if (a[0]!=3) a[0]=3; } else if(position==a.length-1){ if(a[a.length-1]!=player) if(a[a.length-1]!=0) if(a[a.length-1]!=3) a[a.length-1]=3; } int c=0; for(int i=0;i