Four in a row is a game in which players take turns adding tokens to the columns on the game board.   Tokens fall to the lowest position in the chosen column that does not already have a token in it. Once one of the players has placed four of their tokens in a straight line (either vertically, horizontally, or diagonally), they win the game If the board is full and no player has won, then the game ends in a draw.   TASK Using the following class descriptions, create a UML diagram and a version of Four in a row game

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

Four in a row is a game in which players take turns adding tokens to the columns on the game board.

 

Tokens fall to the lowest position in the chosen column that does not already have a token in it. Once one of the players has placed four of their tokens in a straight line (either vertically, horizontally, or diagonally), they win the game

If the board is full and no player has won, then the game ends in a draw.

 

TASK

Using the following class descriptions, create a UML diagram and a version of Four in a row game

 

  • The game must allow for a minimum of two and maximum of four players

  • The game must allow each player to enter their name(duplicate names should not be accepted)

  • The game should give the players the ability to choose how many rows (between four and ten), and how many columns (between four and ten) the game board should have.

  • The code uses several classes, including "Player", "Board","Game" and exceptions for handling errors such as invalid moves and full columns.

Overview

  • Create the Player class, which will handle the players in the game.

  • Create the constructor, which will take in the player's name and player number as arguments and set them to instance variables.

  • Create the getName and getPlayerNumber methods, which will return the player's name and player number, respectively.

  • Create the makeMove method, which will prompt the user for the column where they want to drop their token and return that value.

  • Create a UML class diagram to visualize the relationships between classes and their methods and attributes. You can use a UML tool like Lucidchart, Visual Paradigm, or draw.io to create a class diagram.

 

 

# Board Class
Attribute/Method
board
boardSetUp()
printBoard()
columnFull()
boardFull()
Checks if a given column is full by checking if the top spot of the column is empty.
Checks if the entire board is full by checking if all columns are full.
Adds a token to the board for a given column and player. It checks if the column is full and if it is, it throws
an exception. It also checks if the column number provided is valid and if it is not, it throws an exception.
checklfPlayerls The Winner() Checks if the current player has won the game by getting four of their tokens in a row either horizontally,
vertically, or diagonally by calling other methods.
addToken()
checkVertical()
Description
Keeps track of which player's token (if any) is stored at each location on the game board
Prompts the user for the number of rows and columns for the board and initializes the board with empty
spaces represented by "-"
prints the current state of the board to the console.
checkHorizontal()
Checks if a player has won vertically by iterating through the board array and comparing four consecutive
rows in a column.
Checks if a player has won horizontally by iterating through the board array and comparing four consecutive
columns in a row.
Player Class
Attribute/Method Description
name
playerNumber
Player()
getName()
getNumber()
makeMove()
toString()
Specifies this player's name
Specifies the number of the player's token
Constructor takes a name and playerNumber as parameters and assigns them to the corresponding instance
variables.
Accessor for name
Accessor for playerNumber
Prompts the player for which column they want to place their token in, and returns the column number.
Returns a string representation of the player in the format "Player [player number] is [name]".
Transcribed Image Text:# Board Class Attribute/Method board boardSetUp() printBoard() columnFull() boardFull() Checks if a given column is full by checking if the top spot of the column is empty. Checks if the entire board is full by checking if all columns are full. Adds a token to the board for a given column and player. It checks if the column is full and if it is, it throws an exception. It also checks if the column number provided is valid and if it is not, it throws an exception. checklfPlayerls The Winner() Checks if the current player has won the game by getting four of their tokens in a row either horizontally, vertically, or diagonally by calling other methods. addToken() checkVertical() Description Keeps track of which player's token (if any) is stored at each location on the game board Prompts the user for the number of rows and columns for the board and initializes the board with empty spaces represented by "-" prints the current state of the board to the console. checkHorizontal() Checks if a player has won vertically by iterating through the board array and comparing four consecutive rows in a column. Checks if a player has won horizontally by iterating through the board array and comparing four consecutive columns in a row. Player Class Attribute/Method Description name playerNumber Player() getName() getNumber() makeMove() toString() Specifies this player's name Specifies the number of the player's token Constructor takes a name and playerNumber as parameters and assigns them to the corresponding instance variables. Accessor for name Accessor for playerNumber Prompts the player for which column they want to place their token in, and returns the column number. Returns a string representation of the player in the format "Player [player number] is [name]".
checkHorizontal()
checkRightDiagonal()
checkLeft Diagonal()
Game Class
Attribute/Method
board
players
Game()
setUpGame()
Checks if a player has won horizontally by iterating through the board array and comparing four consecutive
columns in a row.
Checks if a player has won diagonally from left to right by iterating through the board array and comparing
four consecutive diagonal elements starting from the top-left corner.
Checks if a player has won diagonally from right to left by iterating through the board array and comparing
four consecutive diagonal elements starting from the top-right corner.
Description
instance variable of the class, object of Board class representing the board of the game.
instance variable of the class, array of Player objects representing the players of the game.
Constructor for the Game class which initializes the players array and board.
Prompts the user to enter the names of the two players, initializes Player objects for each player, checks that
the players have unique names and sets up the board for the game.
Game Class
Attribute/Method
board
players
Game()
setUpGame()
Description
instance variable of the class, object of Board class representing the board of the game.
instance variable of the class, array of Player objects representing the players of the game.
Constructor for the Game class which initializes the players array and board.
Prompts the user to enter the names of the two players, initializes Player objects for each player, checks that
the players have unique names and sets up the board for the game.
printWinner(Player player) Takes in a Player object and prints out the name of the winner.
playerTurn(Player
currentPlayer)
play()
handles the logic for a player's turn, including getting their move, updating the board, checking for invalid
moves or full columns, and throwing appropriate exceptions.
Controls the overall flow of the game, including alternating turns between players, checking for a winner,
and ending the game when the board is full or a player has four in a row.
Transcribed Image Text:checkHorizontal() checkRightDiagonal() checkLeft Diagonal() Game Class Attribute/Method board players Game() setUpGame() Checks if a player has won horizontally by iterating through the board array and comparing four consecutive columns in a row. Checks if a player has won diagonally from left to right by iterating through the board array and comparing four consecutive diagonal elements starting from the top-left corner. Checks if a player has won diagonally from right to left by iterating through the board array and comparing four consecutive diagonal elements starting from the top-right corner. Description instance variable of the class, object of Board class representing the board of the game. instance variable of the class, array of Player objects representing the players of the game. Constructor for the Game class which initializes the players array and board. Prompts the user to enter the names of the two players, initializes Player objects for each player, checks that the players have unique names and sets up the board for the game. Game Class Attribute/Method board players Game() setUpGame() Description instance variable of the class, object of Board class representing the board of the game. instance variable of the class, array of Player objects representing the players of the game. Constructor for the Game class which initializes the players array and board. Prompts the user to enter the names of the two players, initializes Player objects for each player, checks that the players have unique names and sets up the board for the game. printWinner(Player player) Takes in a Player object and prints out the name of the winner. playerTurn(Player currentPlayer) play() handles the logic for a player's turn, including getting their move, updating the board, checking for invalid moves or full columns, and throwing appropriate exceptions. Controls the overall flow of the game, including alternating turns between players, checking for a winner, and ending the game when the board is full or a player has four in a row.
Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education