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. int rowInput;
  13. int 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. rowInput = myTicTacToe.getRowNumber( input ); // getRowNumber will always return a valid row number
  36. rowInput = myTicTacToe.getRowNumber( input ); // getRowNumber will always return a valid row number
  37.  
  38. /* //validation row
  39. System.out.println( "\nRow and press enter: " );
  40. rowInput = input.next();
  41. while( !(myTicTacToe.isInteger( rowInput )) || (( rowInput < 1 ) || ( rowInput > 3 )){
  42. System.out.println("\n The input has to be a number between 1 and 3, try again");
  43. rowInput = input.next();
  44. }
  45. //end of validation row
  46.  
  47. System.out.println( "\nColumn and press enter: " );
  48. colInput = input.next();
  49. //validation column
  50. while( !(myTicTacToe.isInteger( colInput )) || (( colInput < 1 ) || ( colInput > 3 )){
  51. System.out.println("\n The input has to be a number between 1 and 3, try again");
  52. colInput = input.next();
  53. }
  54. //end of validation column
  55. */
  56.  
  57. rowInput = myTicTacToe.optimizeInput( rowInput );//to subtract 1 from the input
  58. colInput = myTicTacToe.optimizeInput( colInput );//to subtract 1 from the input
  59.  
  60. //check input
  61. squareIsFull = myTicTacToe.checkSquare( rowInput, colInput );
  62. //if the square is taken - true - input again row and column till you find a free one
  63. while( squareIsFull ){
  64. System.out.println( "Square taken, try again. \nEnter the coordinates of the square.\nRow and column - values between 1 and 3." );
  65. System.out.println( "\nRow: " );
  66. rowInput = input.nextInt();
  67. System.out.println( "\nColumn: " );
  68. colInput = input.nextInt();
  69. rowInput = myTicTacToe.optimizeInput( rowInput );//to subtract 1 from the input
  70. colInput = myTicTacToe.optimizeInput( colInput );//to subtract 1 from the input
  71. //check input
  72. squareIsFull = myTicTacToe.checkSquare( rowInput, colInput );
  73. }
  74. //assign the symbol to the chosen square
  75. myTicTacToe.assignSymbol( rowInput, colInput, playerCounter );
  76. //print board
  77. myTicTacToe.printBoard();
  78. //check for draw
  79. victory = myTicTacToe.checkVictory( rowInput, colInput, playerCounter );
  80. //player 1
  81. if(( victory ) && ( myTicTacToe.isOdd( playerCounter ) )){
  82. System.out.println( " Player 1 wins!" );
  83. //print board
  84. //myTicTacToe.printBoard();
  85. break;
  86. }
  87. //player2
  88. else if(( victory ) && !( myTicTacToe.isOdd( playerCounter ) )){
  89. System.out.println( " Player 2 wins!" );
  90. //print board
  91. myTicTacToe.printBoard();
  92. break;
  93. }
  94. playerCounter++;//increment counter to change player's turn
  95. }//end while loop
  96. }//end main
  97. }//end TicTacToeTest class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty