Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***IN JAVA*** PLEASE USE ALL FUNCTIONS PROVIDED For our program, the game will always start with player X, and alternate between players X and O

***IN JAVA***

PLEASE USE ALL FUNCTIONS PROVIDED

For our program, the game will always start with player X, and alternate between players X and O (who will play at the same computer). During each turn, the game will show the players the current board, ask the appropriate player to select a column to add their token to and then check to see if that results in a tie game or a win. If it results in a win, it will inform the players who won, show them the winning board, and ask if they want to play again. If it results in a tie, it will inform the players, show the game board, and ask if they want to play again. If it is neither a tie nor a win, then the program will move onto the next players turn. If a player selects a column that is already full or a column that does not exist (column 15) then the game will inform the player of the error and ask them to choose again. See the sample input and output for more detail. For the first stage of this project we will just be designing our game. You will be creating UML diagrams for classes and methods, as well as writing contracts for each method and class. Designing your system first will allow you to work through the complicated decisions about how your classes and methods interact without having to implement them yet. This will make it easier to implement individual methods later. In order to do this you will need several classes and functions. Please note it is important to follow these directions exactly, to design the classes and functions as they are described, and to use the same class and function names, as well as the same function signatures (return type, and parameter list). This will be important for future assignments as well. Assignments that achieve the overall goal, but do not follow these instructions will lose a substantial amount of points.

GameScreen.java This will be the class that contains the main() function and controls the flow of the game itself. It will alternate the game between players, say whose turn it is, get the column they would like, and place their marker. If the player chooses a location that is not available (either because the column is full or because it is outside the bounds of the 6 x 9 board) then the program will print a message saying the space is unavailable and ask them to enter a new column. Once a placement has been made, it will check to see if either player has won. If so it will print off a congratulatory message, and ask if they would like to play again and prompt them for a response of Y or N. If they choose to play again, it should present them with a blank board and start the game over. If a player has not won, it will check to see if the game has ended in a tie (no spaces left on the board). If the game has ended in a tie, it will print a message saying so, and ask if the player would like to play again. If the game has not ended in a win or a tie, then print the current board to the screen and prompt the next player for their next move. When asking a player for their move, you can assume the user will enter an integer value for the Column. You cannot assume that they will enter a value in range (they could enter 15 or 21). Player X should go first in every game. This class has the least amount of specific requirements. You may design your class and add as many private helper functions that you believe may help you. All input and output to the screen must happen in only this class.

BoardPosition.java This class will be used to keep track of an individual cell for a board. BoardPosition will have variables to represent the Row position, and the Column position. There will only be one constructor for the class, which will take in an int for the Row position, and an int for the Column position. After the constructor has been called, there will be no other way to change any of the fields through any setter functions. Other functions that will be necessary: public int getRow(){ //returns the row } public int getColumn(){ //returns the column } You must also override the equals() method inherited from the Object class. Two BoardPositions are equal if they have the same row and column. You should override the toString() method to create a string in the following format ,. Example 3,5. You wont call these in your code, but it is a best practice to override them and provide a meaningful implementation. No other methods are necessary, and no other methods should be provided.

GameBoard.java This class will represent the game board itself. All attributes of the class must be private unless they are static AND final. The GameBoard itself must be represented as a 2-dimensional array of chars where position [0][0] would be the bottom left of the board and [5][8] would be the top right of the board. This class cannot interact with the users by asking for input or printing output to the screen. The following methods must be publicly available. They must have the exact names, parameters, and return types listed. I have provided contracts for some, but not all of the methods. You must include the provided contracts as well as write contracts for the remaining methods. You must provide Javadoc comments for each method.

public boolean checkIfFree(int c): returns true if column is able to accept another token, false otherwise.

public boolean checkForWin(int c): returns true if the last token placed (which was placed in column c) resulted in the player winning the game. Otherwise, it returns false. Note: this is not checking the entire board for a win, it is just checking if the last token placed results in a win.

public boolean checkTie(): returns true if the game board results in a tie game, otherwise it returns false. Note: use your preconditions to make this much simpler. You should not be checking to see if the board contains any win conditions.

public void placeToken(char p, int c): places a token p in column c on the game board. The token will be placed in the lowest available row in column c.

public boolean checkHorizWin(BoardPosition pos, char p): returns true if the last token placed (which was placed in position pos and player p) resulted in the player winning by getting 5 in a row horizontally. Otherwise, it returns false.

public boolean checkVertWin(BoardPosition pos, char p): returns true if the last token placed (which was placed in position pos and player p) resulted in the player getting 5 in a row vertically. Otherwise, it returns false.

public boolean checkDiagWin(BoardPosition pos, char p): returns true if the last token placed (which was placed in position pos and player p) resulted in the player getting 5 in a row diagonally. Otherwise, it returns false.

public char whatsAtPos(BoardPosition pos): returns the char that is in position pos of the game board. If there is no token at the spot it should return a blank space character: .

public boolean isPlayerAtPos(BoardPosition pos, char player): returns true if the player is at that position. Note: this method will be implemented very similarly to whatsAtPos, but its asking a different question. We only know they will be similar because we know GameBoard will contain a 2D array. If the data structure were to change in the future these two methods could end up being radically different. When designing our class, we dont want to focus on the data structure, but more on the concept it represents and how we would want to interact with it. Its helpful to think about what questions we would ask the object, and Whats at this position? and Is this player at this position? are different questions, so we have different methods for them.

public String toString(): Note: this function should be overriding the method inherited from the Object class. Returns a fully formatted (see example output) string that displays the current game board. This does not print to the screen, it returns a String that represents the game board.

image text in transcribed

image text in transcribed

Sample input and output: 1011121314151617181 Player x, what column do you want to place your marker in? 3 1011121314151617181 X Player o, what column do you want to place your marker in? 3 1011121314151617181 | 101 X Player x, what column do you want to place your marker in? 3 Column is full Player x, what column do you want to place your marker in? 4 1011121314151617181 1 | 101 Il III XIII III 1011 IXIT III 1011 | XX1 Player o, what column do you want to place your marker in? 4 1011121314151617181 1 | 10 | | XIII 1 | 10 11 LXIII || 101011 | XX1 Player x, what column do you want to place your marker in? 5 1011121314151617181 ITT 10 1 X1 | | | 10 | I XII | 10101 | XXXIII Player o, what column do you want to place your marker in? -1 Column cannot be less than 0 Sample input and output: 1011121314151617181 Player x, what column do you want to place your marker in? 3 1011121314151617181 X Player o, what column do you want to place your marker in? 3 1011121314151617181 | 101 X Player x, what column do you want to place your marker in? 3 Column is full Player x, what column do you want to place your marker in? 4 1011121314151617181 1 | 101 Il III XIII III 1011 IXIT III 1011 | XX1 Player o, what column do you want to place your marker in? 4 1011121314151617181 1 | 10 | | XIII 1 | 10 11 LXIII || 101011 | XX1 Player x, what column do you want to place your marker in? 5 1011121314151617181 ITT 10 1 X1 | | | 10 | I XII | 10101 | XXXIII Player o, what column do you want to place your marker in? -1 Column cannot be less than 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

=+How might you explain this phenomenon?

Answered: 1 week ago