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: 313 Weeks Ago, 6 Days 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 09 Nov 2017 My Price 10.00

Make a Tic Tac Toe game. In this game, two players alternate placing

Hi,

I need your help with is java coding.I pretty much as it all set was just wondering if you could help make it so it is the user vs the computer. The Java program is from the game TIC TAC TOE.

 

right now I have it setup for 2 players.

 

part a. Make a Tic Tac Toe game. In this game, two players alternate placing Xs and O into a gred until on play as three matching symbols in a row horizontally, vertically or diagonally. Make a game in which the user is presented with a three by three grid containing the digits through 9. When the user chooses a position by typing a number place an X in the appropriate spot. Generate a random number for the position where the computer will place an O. Do no allow the player or computer to place a symbol where one has already been placed. When either the player or computer has three symbols in a row, declare a winner; if all position has been exhausted and no has three symbols in a row declare a tie.

 

part b. In the Tic Tac Toe application, the computer's selection is chosen randomly, improve the Tic Tac Toe game so that when the computer has two Os in any row, colum or diagonal it selects the winning position for the next move rather than selecting a position randomly Save this as TicTacToe 2

 

Please if you could create it to there are gui interface.

it display a grid with numbers 1 through 9 for each square and when the user press a number it fill in the square.

 

Criteria

 For a properly formatted program (HINT: You can select "Source -> Format") from the IDE menu to get the formatting corect)

 Using a 3x3 array (Your program should have a two dimensional array to represent the 3x3 tic tac toe board)

Correctly funtioning program

 Program must detect when the game is over: either some one wins or there are no board positions left)

 For correctly displaying the board after each turn

 For ensuring that illegal moves cannot be made by the user or by the computer

NOTE: This assignment does not require you to have the computer intelligently selecting the next square. The computer is just
/** ***************************************************************
 ***************************************************************** */
import java.util.Scanner;

public class TicTacToe {

    /*
     * Declare instance variables
     */
    private final char[][] board; //the game board, a 2D array
    private boolean xTurn; //true if X's turn, false if O's turn
    private final Scanner keyboard; //Scanner for reading things in from keyboard

    /*
     * Constructor, sets things up for the game to run
     */
    public TicTacToe() {

        //create the board
        board = new char[3][3];

        //initialize the board to all spaces
        for (int r = 0; r < 3; r++) {

            for (int c = 0; c < 3; c++) {
                board[r][c] = ' ';
            }
        }

        //it's X's turn when we start
        xTurn = true;

        //create our keyboard object
        keyboard = new Scanner(System.in);
    }

    /*
     * Displays a single row of the game board specified by the row parameter
     */
    private void displayRow(int row) {

        System.out.println(" " + board[row][0] + " | " + board[row][1] + " | " + board[row][2]);
    }

    /*
     * Displays the entire game board
     */
    private void displayBoard() {

        displayRow(0);
        System.out.println("-----------");
        displayRow(1);
        System.out.println("-----------");
        displayRow(2);
    }

    /*
     * Displays the basic menu options available to the user
     */
    private void displayMenu() {

        //print whose turn it is
        if (xTurn) {
            System.out.println("X's Turn!");
        } else {
            System.out.println("O's Turn!");
        }

        //print the options menu           
        System.out.println("What would you like to do?");
        System.out.println("1: Make a move");
        System.out.println("2: Start Over");
        System.out.println("3: Quit");
        System.out.print("Choice: ");
    }

    /*
     * Gets the position from the user of where the next move should be made
     * The board is then updated with a valid move
     *
     * Returns true if there is a winner and false if there is no winner
     *
     */
    private boolean getMove() {

        boolean invalid = true;
        int row = 0, column = 0;

        //keep asking for a position until the user enters a valid one
        while (invalid) {

            System.out.println("Which row, column would you like to move to? Enter two numbers between 0-2 separated by a space to indicate position.");
            row = keyboard.nextInt();
            column = keyboard.nextInt();

            //check that the position is within bounds
            if (row >= 0 && row <= 2 && column >= 0 && column <= 2) {

                //check that the position is not already occupied
                if (board[row][column] != ' ') {
                    System.out.println("That position is already taken");
                } else {
                    invalid = false;
                }
            } else {
                System.out.println("Invalid position");
            }
        }

        //fill in the game board with the valid position
        if (xTurn) {
            board[row][column] = 'X';
        } else {
            board[row][column] = 'O';
        }

        return winner(row, column);
    }

    /*
     * Starts the game over by resetting variables
     */
    private void restart() {

        //empty the game board
        for (int r = 0; r < 3; r++) {

            for (int c = 0; c < 3; c++) {
                board[r][c] = ' ';
            }
        }

        //reset whose turn it is
        xTurn = true;
    }

    /*
     * Given the row and column where the last move was made, this method
     * return true if the move resulted in a win and false otherwise
     */
    private boolean winner(int lastR, int lastC) {

        boolean winner = false; //assume there's no winner
        char symbol = board[lastR][lastC]; //the symbol for the last move either X or O

        //check left-right
        //the last move we made was in the row lastR, check that row for three of the same symbol
        int numFound = 0;
        for (int c = 0; c < 3; c++) {
            if (board[lastR][c] == symbol) {
                numFound++;
            }
        }

        if (numFound == 3) {
            winner = true;
        }

        //check up-down
        //the last move we made was in the column lastC, check that column for three of the same symbol
        numFound = 0;
        for (int r = 0; r < 3; r++) {
            if (board[r][lastC] == symbol) {
                numFound++;
            }
        }

        if (numFound == 3) {
            winner = true;
        }

        //check both diagonals
        numFound = 0;
        for (int i = 0; i < 3; i++) {
            if (board[i][i] == symbol) {
                numFound++;
            }
        }

        if (numFound == 3) {
            winner = true;
        }

        numFound = 0;
        for (int i = 0; i < 3; i++) {
            if (board[i][2 - i] == symbol) {
                numFound++;
            }
        }

        if (numFound == 3) {
            winner = true;
        }

        return winner;
    }

    /*
     * Checks whether the board is full and the game is over
     *
     * Return true if full and false otherwise
     */
    private boolean boardFull() {

        //find the number of spots that are occupied by either an X or an O
        int numSpotsFilled = 0;

        for (int r = 0; r < 3; r++) {

            for (int c = 0; c < 3; c++) {
                if (board[r][c] == 'X' || board[r][c] == 'O') {
                    numSpotsFilled++;
                }
            }
        }

        return numSpotsFilled == 9;
    }

    /*
     * Main entry point to the game.  Starts the game.
     */
    public void play() {

        while (true) {

            displayBoard();
            displayMenu();

            int choice = keyboard.nextInt();

            if (choice == 1) {

                if (getMove()) {
                    //we have a winner!
                    displayBoard();    //display board one last time

                    if (xTurn) {
                        System.out.println("X Wins!");
                    } else {
                        System.out.println("O Wins!");
                    }

                    System.exit(0);
                } else if (boardFull()) {
                    //we have a draw
                    displayBoard(); //display board one last time

                    System.out.println("Draw!");

                    System.exit(0);
                } else {
                    //no winner yet
                    xTurn = !xTurn;  //switch whose turn it is
                }
            } else if (choice == 2) {
                restart();
            } else if (choice == 3) {
                System.exit(0);    //quit
            } else {
                System.out.println("Invalid Option");
            }
        }
    }

    public static void main(String[] args) {

        TicTacToe game = new TicTacToe();

        game.play();
    }
}
randomly selecting a square.

Answers

(5)
Status NEW Posted 09 Nov 2017 01:11 PM My Price 10.00

----------- He-----------llo----------- Si-----------r/M-----------ada-----------m -----------Tha-----------nk -----------you----------- fo-----------r y-----------our----------- in-----------ter-----------est----------- an-----------d b-----------uyi-----------ng -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------. P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll -----------be -----------qui-----------ckl-----------y

Not Rated(0)