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