Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.FileReader;
- import java.io.BufferedReader;
- public class TicTacToeGame{
- public static final Character ex = 'X';
- public static final Character oh = 'O';
- public static final Character empty = ' ';
- public static int rows;
- public static int columns;
- public static Character[][] board= new Character[rows][columns];
- public static int win_condition;
- public static boolean human_human_game;
- public static boolean Win;
- public static char player;
- public static char playerSymbol;
- public static boolean pvp;
- /** Checks for a win based on the last symbol played in the game
- *&& board[rows][columns] == ex || board[rows][columns] == oh)
- * It is assumed that the position specified by the last_row_played
- * and last_column_played is valid and that the symbol in the board
- * at that position is not empty. (It must be <em>ex</em> or <em>oh</em>)
- *
- * @param last_row_played is the row of the last symbol played
- * @param last_column_played is the column of the last symbol played
- * @return the length of the winning row/column/diagonal of symbols
- * if the last move is a winning move, or -1 if the last move is not
- * a winning move.
- */
- public static void printBoard() {
- for (int i =0; for i < rows; i +=1 ) {
- System.out.println("_______________");
- System.out.println("|");
- for (int j =0; j < columns; j += 1) {
- System.out.println(board[i][j] + "|");
- }
- System.out.println(empty);
- System.out.println("________________");
- }
- }
- public static void initializeBoard() {
- board = new Character[rows][columns];
- for (int i =0; i < rows; i += 1) {
- for (int j = 0; j < columns; j +=1) {
- board[i][j] = empty;
- }
- }
- }
- public static boolean boardFull() {
- for(int i =0; i < rows; i +=1) {
- for (int j =0; j < columns; j +=1) {
- if (board[i][j].equals(empty)) {
- return false;
- } else {
- return true;
- }
- }
- }
- return boardFull();
- }
- public static void changeBoard(char player, int rows, int columns) {
- board[rows][columns] = player;
- }
- public static boolean Win() {
- for (int i = 1; i < rows -1; i +=1) {
- for ( int j =0; j < columns; j +=1) {
- // 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
- // left and right to see if they won to avoid null errors
- 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)) {
- Win = true;
- }
- }
- }
- for (int i =0; i < rows; i +=1 ){
- for (int j =1; k < columns -1; j +=1) {
- // 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
- // this is to avoid null errors
- 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)) {
- Win = true;
- }
- }
- }
- for (int i =1; i < rows-1; i+=1) {
- for (int j =0; j < columns -1; j +=1) {
- // checks for diagonals and its bound by i and j to nvr check outside the board
- 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)) {
- Win = true;
- }
- 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)) {
- Win = true;
- }
- }
- }
- return Win;
- }
- public static boolean Tie() {
- for (int i =0; i < rows; i +=1 ) {
- for (int j =0; j < rows; j +1 ) {
- if(board[i][j].equals(empty)) { // if there is an empty cell on the board, the game continues
- return false;
- } else {
- return true;
- }
- }
- }
- return Tie();
- }
- public static void main(String[] args){
- /*
- * handleWin command line arguments if any
- * to determine if the game is human-human
- * or human-computer
- *------------------------------------------*/
- /* there are no command line argument present */
- // add your code here
- /*------------------------------------
- * read N-M-K data from init file
- * N = rows
- * M = columns
- * K = win_condition
- *------------------------------------*/
- /*-------------------------------------//Scanner l.scan = new Scanner (System.in);
- *-------------------------------------Win
- * BEGIN : Do NOT change the code here
- *-------------------------------------*/
- BufferedReader file_input;
- FileReader file;
- String file_name = "init";
- String line;
- try{
- file = new FileReader(file_name);
- file_input = new BufferedReader(file);
- line = file_input.readLine();
- rows = Integer.parseInt(line);
- line = file_input.readLine();
- columns = Integer.parseInt(line);
- line = file_input.readLine();
- win_condition = Integer.parseInt(line);
- /* always close your files you are done with them! */
- file_input.close();
- }catch(Exception e){
- /* somethine went wrong! */
- System.err.println("Failure to read data from init file properly");
- System.err.println(e);
- System.err.println("Program ending");
- return;
- }
- /*-------------------------------------
- * END : Do NOT change the code here
- *-------------------------------------
- *-------------------------------------*/
- // add your code here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement