1. Homepage
  2. Programming
  3. ECE244 Programming Fundamentals - Assignment Connect Four

ECE244 Programming Fundamentals - Assignment Connect Four

Engage in a Conversation
TorontoECE244Programming FundamentalsConnect FourC++

Objectives

The main objective of this assignment is to introduce you to the use of classes, objects, and methods, thus applying the related concepts presented in the lectures. You will do so by building a Connect Four game. You will first implement a class that represents the state of the game and a function that implements the logic of the game. You will then write a main program that uses the class and function to run the game. CourseNana.COM

Connect Four

Connect Four is a game, where two players, Red (R) and Yellow (Y), take turns placing a colored piece in any column of a 7×7 board. One thing to remember is that pieces will fall to the lowest available space in the column. The player that succeeds in placing four of their pieces in a straight line (horizontal, vertical or diagonal) wins the game. The player with the Red (R) pieces always makes the first move. The following example shows a state of a game won by player with R. CourseNana.COM

Game Overview

The game consists of two main components: the game controller or simply the controller and the game logic. This is shown pictorially in Figure 2. The controller is responsible for collecting players’ input to determine the column that a player wishes to place a piece in and determining whether it is valid or not. It is also responsible for displaying the game board in the text format described later in this handout. The controller is implemented by the main program of the game. The game logic is implemented through a single function called playMove(). This function is responsible for “playing” the move, such as actually placing the piece in the game board, checking for a winner and changing the player’s turn. CourseNana.COM

The controller and the game logic interact using an object of the type GameState. This object stores the state of the game, including the column selected by a player, the row the piece falls into, the marks at each board location (i.e., R or Y) , whether the move is valid or not, whose turn it is, whether the game is over or not, etc. The game operation is simple. The controller (or main function) prompts a player to enter one integer, representing the column of the board. If the column is valid (see below), it updates the CourseNana.COM

GameState object with the row and column coordinates and invokes the playMove() function of the game logic, passing to it (by reference) the GameState object. The playMove() function updates the GameState object based on the move of the player, for example, if there is a winner, who is it. When the function returns to the controller, it prints the updated GameState. The process repeats until there is a win or draw. CourseNana.COM

Problem Statement

For this lab, you will create a best-of-3 game of Connect Four. This means that one player needs to win 2 individual games of Connect Four to win the match. Typically in a best-of-3 for any game, there are a lot of complicated measures to ensure fairness, for example, switching sides after every game. For this lab however, you can assume that the player that starts with Red will stay as Red for every game, and will always go first. In other words, the sides never switch. On top of that, you do not need to worry about draws. We will NOT test for these cases, but you can implement your own logic that handles draws if you want. In order to do so, you will implement the methods of the class GameState, as defined in GameState.h. You will also implement a function playMove(), which “plays” the move indicated by a player’s input. The remainder of this section describes: (1) the GameState class, (2) the methods of which you must implement, (3) the playMove() function that implements the game logic and (3) the controller functionality implemented in main. CourseNana.COM

The GameState Class

The state of a Connect Four game is represented by an instance of the GameState class. This object is created, initialized, and eventually destroyed by the controller. The definition of this class appears in the file GameState.h, which is released with the assignment. You may NOT modify this file to add to or delete from its content. Modifying the file often results in a mark of zero for the assignment. The class contains the following data members: CourseNana.COM

•gameBoard is a boardSize×boardSize (7×7 in this assignment) two-dimensional array that represents the game board. gameBoard[x][y] contains the information of column x and row y. It stores the marks of each player, and thus the state of the game. Each element of the array can be one of Empty, R or Y . The elements of the array are initialized by the controller to Empty. The definitions of boardSize, Empty, R and Y appear in the globals.h, which is also part of the assignment release. You may NOT modify this file to add to or delete from its content. Modifying the file often results in a mark of zero for the assignment. In the array, gameBoard[0][0] represents the bottom-left corner cell of the game grid. Similarly, gameBoard[boardSize-1][boardSize-1] represents the top-right corner cell of the board. This effectively defines the row and column coordinates of each cell in the grid. •selectedColumn is the integer that is selected by the player. •selectedRow is the integer that represents the ”lowest” row available in the column that was selected by the player. •moveValid is a Boolean variable that is set to true when the selected coordinates represent a valid move for the current game. That is, it is set to true when the column at selectedColumn is not full and is between 0 and boardSize - 1. Otherwise, moveValid is set to false. •gameOver is a Boolean variable that should be set to true if the game is over as a result of the a win and to false •turn is a Boolean variable that indicates whose turn it is, R (true) or Y (false) for the current selected coordinates. If the move is valid, then the value of turn should be changed by the game logic, i.e. in playMove(), from true to false or from false to true to reflect the change in turn. The controller (or main) does not use this variable at all. •winner is an integer that tells us who the winner is, where Empty is used to indicate neither (no winner yet). The GameState class provides accessors and mutators to the respective class data members. Please read the comments in the GameState.h file to find out what these methods do. At this point in reading this handout, you can complete GameState.cpp. CourseNana.COM

The playMove() Function

The game logic implements a single function: void playMove(GameState& game_state)This function is to be implemented in the file playMove.cpp and it is called every time a player makes a move. The function is called only if the move is valid. Its goal is to “play” the move and update the GameState object that is passed by reference to function. Upon completion, the function must update the game state object by updating: •The turn value to reflect that the turn has changed. •The Boolean variable moveValid, described earlier. •The Boolean variable gameOver to either true or false to reflect if the move ends the game in either a win or a draw. •The winner variable to either R, Y or Empty. The game board example shown in Figure 3 is used to demonstrate the expected updates to the game state object. For this game grid, the turn variable is true, indicating that it is R’s turn to play. If selectedColumn = 3, we need to find the lowest possible row to place the piece (in this case the second row) and then set selectedRow = 1 (since we count from 0). Then, an R piece is placed in the second row and fourth column of the game board. The move is already valid and (validMove is set to true). When the playMove() function is called, checking if the game is over and who is the winner should occur. In this case, the game is not over, and (gameOver is set to false). The variable turn is changed from true to false to indicate that it is now Y’s turn to play. CourseNana.COM

The Game Controller

The game controller is implemented in the main() function of the game, contained in the file connectfour.cpp. Its purpose is to first create and initialize 3 game boards for the best-of-3. This is done by creating an array of 3 GameState objects. Note that when creating an array of objects, the default constructor will be called for each object in that array. Once the game boards are initialized, for each GameState you should: •Prompt the player for an integer representing the column of the game board that the player wishes to place their piece in. •Check that the player enters an integer that is in the range of 0–6 and  that the column is not full. CourseNana.COM

•Print the selected column if it is valid. Otherwise, print "Invalid column", and give the player another chance till they enter a valid column. •Find the lowest available row that the piece will fall to, and then set the selectedColumn and selectedRow of the GameState object to the entered values. •Update the game board at the appropriate location by either R or Y . •Call the playMove(). •Print the gameBoard of the GameState. •Announce the winner if there is one, and repeat for another game, untill a player wins best-of-3 games. A simple example run is given in the Appendix of this handout. CourseNana.COM

Rationelle

One might question the purpose of implementing a GameState class, as opposed to having only functions. As you work on this lab, you will notice that all you need to pass to functions is an instance of GameState class. Also, having only functions might be simpler to do if we were to play only one game. However, if we were to have a rounds of games, such as in a best-of-3 or for a tournament database that needs information on multiple games, it makes sense to use store the state of each game in an instance of GameState class. One might also question why some functions are defined outside the GameState class. For example, playMove() is not a method of GameState, yet the method only accesses data from GameState instance, so why not make it a method of GameState? The designers of ConnectFour decided that GameState instances should only keep track of the state of the game, but should not know anything about how the game is played. This would allow other types of games to be played on the same type of board and be implemented using the same GameState class. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Toronto代写,ECE244代写,Programming Fundamentals代写,Connect Four代写,C++代写,Toronto代编,ECE244代编,Programming Fundamentals代编,Connect Four代编,C++代编,Toronto代考,ECE244代考,Programming Fundamentals代考,Connect Four代考,C++代考,Torontohelp,ECE244help,Programming Fundamentalshelp,Connect Fourhelp,C++help,Toronto作业代写,ECE244作业代写,Programming Fundamentals作业代写,Connect Four作业代写,C++作业代写,Toronto编程代写,ECE244编程代写,Programming Fundamentals编程代写,Connect Four编程代写,C++编程代写,Torontoprogramming help,ECE244programming help,Programming Fundamentalsprogramming help,Connect Fourprogramming help,C++programming help,Torontoassignment help,ECE244assignment help,Programming Fundamentalsassignment help,Connect Fourassignment help,C++assignment help,Torontosolution,ECE244solution,Programming Fundamentalssolution,Connect Foursolution,C++solution,