fork download
  1. /*TicTacToeTest.java*/
  2. import java.util.Scanner;
  3.  
  4. public class TicTacToeTest{
  5.  
  6. public static void main( String[] args ){
  7. boolean isFull = false;//if true all the squares have been assigned.
  8. boolean isDraw = false;//to check if the game is draw
  9. boolean squareIsFull = false;//returns the status of a square, if false the square is full if true the square is empty
  10. boolean victory = false;//if returns true one of the players has won
  11. int playerCounter = 1;//if it is odd it's player1's turn if even it's player2's turn
  12. String rowInput;
  13. String colInput;
  14. Scanner input = new Scanner( System.in );
  15. TicTacToe myTicTacToe = new TicTacToe();//create object
  16. myTicTacToe.printBoard();
  17. while( !isFull ){
  18. isDraw = myTicTacToe.isDraw();
  19. if( isDraw ){
  20. System.out.println( "The game is draw!\n" );
  21. //print board
  22. //myTicTacToe.printBoard();
  23. break;
  24. }
  25. //print board
  26. //myTicTacToe.printBoard();
  27. //System.out.println("the number is " + playerCounter );
  28. if( !(myTicTacToe.isOdd( playerCounter ))){//player 1
  29. System.out.println( "\nPlayer 1: enter the coordinates of the square.\nRow and column - values between 1 and 3." );
  30. }
  31. else{//player 2
  32. System.out.println( "\nPlayer 2: enter the coordinates of the square.\nRow and column - values between 1 and 3." );
  33. }
  34.  
  35. System.out.println( "\nRow and press enter: " );
  36. rowInput = input.next();
  37. //validation row
  38. while( !(myTicTacToe.isInteger( rowInput )) || (( rowInput < 1 ) || ( rowInput > 3 )){
  39. System.out.println("\n The input has to be a number between 1 and 3, try again");
  40. rowInput = input.next();
  41. }
  42. //end of validation row
  43.  
  44. System.out.println( "\nColumn and press enter: " );
  45. colInput = input.next();
  46. //validation column
  47. while( !(myTicTacToe.isInteger( colInput )) || (( colInput < 1 ) || ( colInput > 3 )){
  48. System.out.println("\n The input has to be a number between 1 and 3, try again");
  49. colInput = input.next();
  50. }
  51. //end of validation column
  52. rowInput = myTicTacToe.optimizeInput( rowInput );//to subtract 1 from the input
  53. colInput = myTicTacToe.optimizeInput( colInput );//to subtract 1 from the input
  54.  
  55. //check input
  56. squareIsFull = myTicTacToe.checkSquare( rowInput, colInput );
  57. //if the square is taken - true - input again row and column till you find a free one
  58. while( squareIsFull ){
  59. System.out.println( "Square taken, try again. \nEnter the coordinates of the square.\nRow and column - values between 1 and 3." );
  60. System.out.println( "\nRow: " );
  61. rowInput = input.nextInt();
  62. System.out.println( "\nColumn: " );
  63. colInput = input.nextInt();
  64. rowInput = myTicTacToe.optimizeInput( rowInput );//to subtract 1 from the input
  65. colInput = myTicTacToe.optimizeInput( colInput );//to subtract 1 from the input
  66. //check input
  67. squareIsFull = myTicTacToe.checkSquare( rowInput, colInput );
  68. }
  69. //assign the symbol to the chosen square
  70. myTicTacToe.assignSymbol( rowInput, colInput, playerCounter );
  71. //print board
  72. myTicTacToe.printBoard();
  73. //check for draw
  74. victory = myTicTacToe.checkVictory( rowInput, colInput, playerCounter );
  75. //player 1
  76. if(( victory ) && ( myTicTacToe.isOdd( playerCounter ) )){
  77. System.out.println( " Player 1 wins!" );
  78. //print board
  79. //myTicTacToe.printBoard();
  80. break;
  81. }
  82. //player2
  83. else if(( victory ) && !( myTicTacToe.isOdd( playerCounter ) )){
  84. System.out.println( " Player 2 wins!" );
  85. //print board
  86. myTicTacToe.printBoard();
  87. break;
  88. }
  89. playerCounter++;//increment counter to change player's turn
  90. }//end while loop
  91. }//end main
  92. }//end TicTacToeTest class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty