SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 1 Day Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 31 Oct 2017 My Price 10.00

Use printwriter to outputs the game record to a txt.file!

Java Programming] I created a TicTacToe game.

Please help me to

  1. separate them into two different classes(a driver program including main method)
  2. Use printwriter to outputs the game record to a txt.file!

Thank youimport java.util.Scanner;
/**
 * Design a traditional Tic-Tac-Toc game
 * This game needs two players
 * The program will end when there is a winner or the game is draw
 */
public class The_Real_TicTacToe_Game{
   
   public static Scanner keyboard = new Scanner(System.in); // Create an input Scanner
   public static String[][] numberTable={{"1 1","1 2","1 3"},{"2 1","2 2","2 3"},{"3 1","3 2","3 3"}}; //sample grid
   
   // User Names
   private static String playerOneName = null;
   private static String playerTwoName = null;
   
   // Name-constants to represent the seeds and cell contents
   private static final int EMPTY = 0;
   private static final int PLAYER_ONE= 1;
   private static final int PLAYER_TWO = 2;
 
   //Draw or win
   private static final int PLAYING = 0;
   private static final int DRAW = 1;
   private static final int POne_WON = 2;
   private static final int PTwo_WON = 3;
 
   // The game board and the game status
   public static final int ROWS = 3, COLS = 3; // number of rows and columns
   public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
                                                      //  containing (EMPTY, CROSS, NOUGHT)
   public static int currentState;  // the current state of the game
                                    // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
   public static int currentPlayer; // the current player (CROSS or NOUGHT)
   public static int currntRow, currentCol; // current seed's row and column
 
   public static void main(String[] args) {
   
      // Initialize the game-board and current status
      setName();
      display(numberTable);
      startTheGame();
      // Play the game once
      do {
       playerMove(currentPlayer); // update currentRow and currentCol
       updateGame(currentPlayer, currntRow, currentCol); // update currentState
       printBoard();
       declareWinner();
         // Switch player
       currentPlayer = (currentPlayer == PLAYER_ONE) ? PLAYER_TWO : PLAYER_ONE;
      } while (currentState == PLAYING); // repeat if not game-over
    }

   
   /* Check if there is a winner; declare the winner and end the game
    * Check if its a draw; notice the players
    * Otherwise, keep playing!
   */
   public static void declareWinner()
   {
       if (currentState == POne_WON) {
            System.out.println( "Congratulations!" + playerOneName + " has won the game");
         } else if (currentState == PTwo_WON) {
            System.out.println( "Congratulations!" + playerTwoName + " has won the game");;
         } else if (currentState == DRAW) {
            System.out.println("Oops... It's a Draw");
         }
    }
   
   // Greetings
   public static void setName()
   {System.out.println("Hello gamers!");
    System.out.println("Welcome to the game of TicTaceToe!");
    System.out.println("This game requires two players, please enter your names!");
    System.out.println("Player 'X', what is your name:");
    playerOneName = keyboard.nextLine();
    System.out.println("Player 'O', what is your name:");
    playerTwoName = keyboard.nextLine();}
    
   public static String getPlayerOneName()
    {return playerOneName;}
    
   public static String getPlayerTwoName()
    {return playerTwoName;}
   
    // Display the table
   private static void display(String[][] table)
        {
                System.out.println("Here is our grid, please follow the instruction,");
                System.out.println("and type the row and column number seperated with whitespace!");
            for(int i=0;i<3;i++)
                {
                        System.out.println("\n-------------");
                        System.out.print("|");
                        for(int j=0;j<3;j++)
                                System.out.print(table[i][j]+"|");
                }
                System.out.println("\n-------------");
        }
   /** Initialize the game-board contents and the current states */
   public static void startTheGame() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            board[row][col] = EMPTY;  
         }
      }
      currentState = PLAYING; // ready to play
      currentPlayer = PLAYER_ONE;  // cross plays first
   }
 
   /** Player with the "theSeed" makes one move, with input validation.
       Update global variables "currentRow" and "currentCol". */
   public static void playerMove(int theSeed) {
      boolean validInput = false;  // for input validation
      do {
         if (theSeed == PLAYER_ONE) {
            System.out.print(playerOneName + ", enter your move: ");
         } else {
            System.out.print(playerTwoName + ",enter your move: ");
         }
         int row = keyboard.nextInt() - 1;  // array index starts at 0 instead of 1
         int col = keyboard.nextInt() - 1;
         if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
            currntRow = row;
            currentCol = col;
            board[currntRow][currentCol] = theSeed;  // update game-board content
            validInput = true;  // input okay, exit loop
         } else {
            System.out.println("This move at (" + (row + 1) + "," + (col + 1)
                  + ") is not valid. Try again...");
         }
      } while (!validInput);  // repeat until input is valid
   }
 
   /** Update the "currentState" after the player with "theSeed" has placed on
       (currentRow, currentCol). */
   public static void updateGame(int theSeed, int currentRow, int currentCol) {
      if (checkForWin()) {  // check if there is any winners
         currentState = (theSeed == PLAYER_ONE) ? POne_WON : PTwo_WON;
      } else if (isDraw()) {  // check is the gird is full
         currentState = DRAW;
      }
      // Otherwise keep playing
   }
 
   /** Return true if the table is full*/
   // TODO: Shall declare draw if no player can "possibly" win
   public static boolean isDraw() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            if (board[row][col] == EMPTY) {
               return false;  // an empty cell found, not draw, exit
            }
         }
      }
      return true;  // no empty cell, it's a draw
   }
   /** Print the game board */
   public static void printBoard() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            printCell(board[row][col]); // print each of the cells
            if (col != COLS - 1) {
               System.out.print("|");   // print vertical partition
            }
         }
         System.out.println();
         if (row != ROWS - 1) {
            System.out.println("-----------"); // print horizontal partition
         }
      }
      System.out.println();
   }
 
   /** Print a cell with the specified "content" */
   public static void printCell(int content) {
      switch (content) {
         case EMPTY:  System.out.print("   "); break;
         case PLAYER_TWO: System.out.print(" O "); break;
         case PLAYER_ONE:  System.out.print(" X "); break;
      }
   }
   
   
   // Check if any players won after placing the very last one
   // Only three kinds of win : Rows / Columns / Diagonals have the same value
   public static boolean checkForWin() {
        return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());
    }
    
    
    // Check if any players win by rows
    public static boolean checkRowsForWin() {
        for (int i = 0; i < 3; i++) {
            if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
                return true;
            }
        }
        return false;
    }
    
    
    // Check if any players win by columns
    public static boolean checkColumnsForWin() {
        for (int i = 0; i < 3; i++) {
            if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
                return true;
            }
        }
        return false;
     }
    
    // Check if any players win by diagonals
    public static boolean checkDiagonalsForWin() {
        return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true)
             || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
    }
    
    // Check if three continuous values are the same
    public static boolean checkRowCol(int c1, int c2, int c3) {
        return ((c1 != 0) && (c1 == c2) && (c2 == c3));
    }
    

 }

Answers

(5)
Status NEW Posted 31 Oct 2017 02:10 PM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)