Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;
  4.  
  5. public class TicTacToeGame{
  6. public static final Character ex = 'X';
  7. public static final Character oh = 'O';
  8. public static final Character empty = ' ';
  9. public static int rows;
  10. public static int columns;
  11. public static Character[][] board= new Character[rows][columns];
  12. public static int win_condition;
  13. public static boolean human_human_game;
  14. public static boolean Win;
  15. public static char player;
  16. public static char playerSymbol;
  17. public static boolean pvp;
  18.  
  19. /** Checks for a win based on the last symbol played in the game
  20. *&& board[rows][columns] == ex || board[rows][columns] == oh)
  21. * It is assumed that the position specified by the last_row_played
  22. * and last_column_played is valid and that the symbol in the board
  23. * at that position is not empty. (It must be <em>ex</em> or <em>oh</em>)
  24. *
  25. * @param last_row_played is the row of the last symbol played
  26. * @param last_column_played is the column of the last symbol played
  27. * @return the length of the winning row/column/diagonal of symbols
  28. * if the last move is a winning move, or -1 if the last move is not
  29. * a winning move.
  30. */
  31.  
  32. public static void printBoard() {
  33.  
  34. for (int i =0; for i < rows; i +=1 ) {
  35. System.out.println("_______________");
  36. System.out.println("|");
  37.  
  38. for (int j =0; j < columns; j += 1) {
  39. System.out.println(board[i][j] + "|");
  40.  
  41. }
  42. System.out.println(empty);
  43. System.out.println("________________");
  44. }
  45. }
  46.  
  47. public static void initializeBoard() {
  48.  
  49. board = new Character[rows][columns];
  50.  
  51. for (int i =0; i < rows; i += 1) {
  52. for (int j = 0; j < columns; j +=1) {
  53. board[i][j] = empty;
  54. }
  55. }
  56. }
  57.  
  58. public static boolean boardFull() {
  59.  
  60. for(int i =0; i < rows; i +=1) {
  61. for (int j =0; j < columns; j +=1) {
  62.  
  63. if (board[i][j].equals(empty)) {
  64. return false;
  65. } else {
  66. return true;
  67. }
  68. }
  69. }
  70. return boardFull();
  71. }
  72.  
  73. public static void changeBoard(char player, int rows, int columns) {
  74. board[rows][columns] = player;
  75. }
  76.  
  77. public static boolean Win() {
  78.  
  79. for (int i = 1; i < rows -1; i +=1) {
  80. for ( int j =0; j < columns; j +=1) {
  81. // if the cell is to the right of the left most cell and if the cell is to the left of the right most cell check its
  82. // left and right to see if they won to avoid null errors
  83. if (board[i][j] == board[i][j] && board[i][j] == board[i+1][j] && board[i-1][j] == board[i +1][j] && (board[i][j] == ex || board[i][j] == oh)) {
  84. Win = true;
  85. }
  86. }
  87. }
  88.  
  89. for (int i =0; i < rows; i +=1 ){
  90. for (int j =1; k < columns -1; j +=1) {
  91. // if the cell is beneath the topmost cell and above the bottom most cell check above and below it for x's and o's
  92. // this is to avoid null errors
  93. if (board[i][j] == board[i][j-1] && board[i][j] == board[i][j +1]&& board[i][j-1] == board[i][j+1] && (board[i][j] == ex || board[i][j] == oh)) {
  94. Win = true;
  95. }
  96. }
  97. }
  98.  
  99. for (int i =1; i < rows-1; i+=1) {
  100. for (int j =0; j < columns -1; j +=1) {
  101. // checks for diagonals and its bound by i and j to nvr check outside the board
  102. if (board[i][j] == board[i+1][j-1] && board[i][j] == board[i-1][j+1] && board[i+1][j-1] == board[i-1][j+1] && (board[i][j] == ex || board[i][j] == oh)) {
  103. Win = true;
  104. }
  105.  
  106. if (board[i][j] == board[i-1][j-1] && board[i][j] == board[i+1][j+1] && board[i+1][j+1] == board[i-1][j-1] && (board[i][j] == ex || board[i][j] == oh)) {
  107. Win = true;
  108.  
  109. }
  110. }
  111. }
  112. return Win;
  113. }
  114.  
  115. public static boolean Tie() {
  116.  
  117. for (int i =0; i < rows; i +=1 ) {
  118. for (int j =0; j < rows; j +1 ) {
  119.  
  120. if(board[i][j].equals(empty)) { // if there is an empty cell on the board, the game continues
  121. return false;
  122.  
  123. } else {
  124. return true;
  125. }
  126. }
  127. }
  128. return Tie();
  129. }
  130.  
  131.  
  132.  
  133. public static void main(String[] args){
  134.  
  135.  
  136. /*
  137. * handleWin command line arguments if any
  138. * to determine if the game is human-human
  139. * or human-computer
  140. *------------------------------------------*/
  141.  
  142. /* there are no command line argument present */
  143.  
  144. // add your code here
  145.  
  146. /*------------------------------------
  147. * read N-M-K data from init file
  148. * N = rows
  149. * M = columns
  150. * K = win_condition
  151. *------------------------------------*/
  152.  
  153. /*-------------------------------------//Scanner l.scan = new Scanner (System.in);
  154. *-------------------------------------Win
  155. * BEGIN : Do NOT change the code here
  156. *-------------------------------------*/
  157. BufferedReader file_input;
  158. FileReader file;
  159. String file_name = "init";
  160. String line;
  161.  
  162. try{
  163. file = new FileReader(file_name);
  164. file_input = new BufferedReader(file);
  165.  
  166. line = file_input.readLine();
  167. rows = Integer.parseInt(line);
  168.  
  169. line = file_input.readLine();
  170. columns = Integer.parseInt(line);
  171.  
  172. line = file_input.readLine();
  173. win_condition = Integer.parseInt(line);
  174.  
  175. /* always close your files you are done with them! */
  176. file_input.close();
  177.  
  178. }catch(Exception e){
  179. /* somethine went wrong! */
  180. System.err.println("Failure to read data from init file properly");
  181. System.err.println(e);
  182. System.err.println("Program ending");
  183. return;
  184. }
  185.  
  186.  
  187. /*-------------------------------------
  188. * END : Do NOT change the code here
  189. *-------------------------------------
  190. *-------------------------------------*/
  191.  
  192. // add your code here
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement