Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicTacToe {
  4. public static char X = 'X';
  5. public static char O = 'O';
  6. public static char B = ' ';
  7.  
  8. public static void main(String[] args) {
  9. /*
  10. * Squares are associated with following variables.
  11. * 1 2 3
  12. * 1 | a | b | c |
  13. * 2 | d | e | f |
  14. * 3 | g | h | i |
  15. */
  16. // Initialize squares to blank.
  17.  
  18. char a = B, b = B, c = B, d = B, e = B, f = B, g = B, h = B, i = B;
  19.  
  20. Scanner input = new Scanner(System.in);
  21.  
  22. System.out.println("Welcome to Tic-tac-toe");
  23.  
  24. // Get player's names.
  25. System.out.print("Enter player 1's name? ");
  26. String player1 = input.nextLine();
  27. System.out.print("Enter player 2's name? ");
  28. String player2 = input.nextLine();
  29.  
  30. // Print blank board.
  31. printBoard(a, b, c, d, e, f, g, h, i);
  32.  
  33. for (int move = 0; move < 9; move++) {
  34. char turn;
  35.  
  36. // Check whose turn it is...
  37. if (move % 2 == 0) {
  38. System.out.println("It is " + player1 + "'s turn...");
  39. turn = X;
  40. } else {
  41. System.out.println("It is " + player2 + "'s turn...");
  42. turn = O;
  43. }
  44.  
  45. // Choose a square.
  46. System.out.print("Pick a row number: ");
  47. int row = input.nextInt();
  48. System.out.print("Pick a column number: ");
  49. int column = input.nextInt();
  50.  
  51. // Based on whose turn it is and the picked square, fill in either an X or an O.
  52. if (row == 1 && column == 1 && a == B) {
  53. a = turn;
  54. } else if (row == 1 && column == 2 && b == B) {
  55. b = turn;
  56. } else if (row == 1 && column == 3 && c == B) {
  57. c = turn;
  58. } else if (row == 2 && column == 1 && d == B) {
  59. d = turn;
  60. } else if (row == 2 && column == 2 && e == B) {
  61. e = turn;
  62. } else if (row == 2 && column == 3 && f == B) {
  63. f = turn;
  64. } else if (row == 3 && column == 1 && g == B) {
  65. g = turn;
  66. } else if (row == 3 && column == 2 && h == B) {
  67. h = turn;
  68. } else if (row == 3 && column == 3 && i == B) {
  69. i = turn;
  70. } else {
  71. System.out.println("You entered incorrect input! Try again...");
  72. move--;
  73. }
  74.  
  75. // Print board.
  76. printBoard(a, b, c, d, e, f, g, h, i);
  77.  
  78. // Check if there is a winner.
  79. checkWinner(a, b, c, d, e, f, g, h, i, player1, player2);
  80. }
  81.  
  82. // We played all 9 moves and no one has won!
  83. System.out.println("Stalemate!");
  84. }
  85.  
  86.  
  87. public static void checkWinner(
  88. char a,
  89. char b,
  90. char c,
  91. char d,
  92. char e,
  93. char f,
  94. char g,
  95. char h,
  96. char i,
  97. String player1,
  98. String player2
  99. ) {
  100. // Either X or O.
  101. char winnerType;
  102.  
  103. if (a == b && b == c) {
  104. // First row.
  105. winnerType = a;
  106. } else if (d == e && e == f) {
  107. // Second row.
  108. winnerType = d;
  109. } else if (g == h && h == i) {
  110. // Third row.
  111. winnerType = g;
  112. } else if (a == d && d == g) {
  113. // First column.
  114. winnerType = a;
  115. } else if (b == e && e == h) {
  116. // Second column.
  117. winnerType = b;
  118. } else if (c == f && f == i) {
  119. // Third column.
  120. winnerType = c;
  121. } else if (a == e && b == i) {
  122. // First diagonal.
  123. winnerType = a;
  124. } else if (g == e && e == c) {
  125. // Second diagonal.
  126. winnerType = g;
  127. } else {
  128. return;
  129. }
  130.  
  131. // Check if someone won and end the game.
  132. if (winnerType == X) {
  133. System.out.println(player1 + " won the game!!!");
  134. System.exit(0);
  135. } else if (winnerType == O) {
  136. System.out.println(player2 + " won the game!!!");
  137. System.exit(0);
  138. }
  139. }
  140.  
  141. public static void printBoard(
  142. char a,
  143. char b,
  144. char c,
  145. char d,
  146. char e,
  147. char f,
  148. char g,
  149. char h,
  150. char i
  151. ) {
  152. // Print the board in a tabular form.
  153. System.out.println();
  154. System.out.println(" \t \t1\t \t2\t \t3");
  155. System.out.println("1\t|\t" + a + "\t|\t" + b + "\t|\t" + c + "\t|\t");
  156. System.out.println("2\t|\t" + d + "\t|\t" + e + "\t|\t" + f + "\t|\t");
  157. System.out.println("3\t|\t" + g + "\t|\t" + h + "\t|\t" + i + "\t|\t");
  158. System.out.println();
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement