0% found this document useful (0 votes)
42 views22 pages

DualGameProject FoP Group05

The document describes a project that combines the games of Tic-Tac-Toe and Chess into a single application. It aims to allow users to seamlessly switch between the simplicity of Tic-Tac-Toe and complexity of Chess. The project uses SDL2 for graphics and includes features like a main menu, separate game screens, input handling, graphics/animation, and multiplayer mode. It divides the work into modules for single/double player Tic-Tac-Toe and human vs human Chess. It provides descriptions of the header files and functions used to implement gameplay logic, rendering, event handling, and navigation between games.

Uploaded by

arafatahmmed51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views22 pages

DualGameProject FoP Group05

The document describes a project that combines the games of Tic-Tac-Toe and Chess into a single application. It aims to allow users to seamlessly switch between the simplicity of Tic-Tac-Toe and complexity of Chess. The project uses SDL2 for graphics and includes features like a main menu, separate game screens, input handling, graphics/animation, and multiplayer mode. It divides the work into modules for single/double player Tic-Tac-Toe and human vs human Chess. It provides descriptions of the header files and functions used to implement gameplay logic, rendering, event handling, and navigation between games.

Uploaded by

arafatahmmed51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

University of Dhaka

Department of Computer Science and


Engineering
Project Report
Fundamentals of Programming

Project Name
Dualgame : Tic-Tac-Toe and Chess

GROUP NUMBER :05

Team Members

MD. Abu Horaira(26)


MD. Adib Ahsan (60)
Shraban Karmoker Avi(44)
Introduction
In the realm of game development, creating engaging and diverse
gaming experiences is a constant pursuit. This project aims to
combine the classic and strategic elements of two timeless games,
Tic Tac Toe and Chess, into a single application. The
implementation will be done using SDL2, a powerful cross- platform
development library that provides low-level access to audio,
keyboard, mouse, and display.

Objectives:
Integration of Two Games:
Combine the simplicity of Tic Tac Toe with the complexity of
Chess within a single application.
Allow users to seamlessly switch between the two games.
Graphical User Interface (GUI):
Implement an intuitive and visually appealing GUI using SDL2.
Ensure user-friendly navigation and interaction.
Game Logic:
Implement the rules and logic for both Tic Tac Toe and Chess.
Develop an algorithm for legal moves with programming
language (c and c++) and game outcomes.
Multiplayer Support:
Facilitate two-player mode for both games.
Allow users to play against each other on the same device.
Project Features:
Main Menu:
• A central hub for users to choose between Tic Tac Toe and
Chess.
Game Screens:
• Separate screens for each game, with dedicated interfaces
tailored to their specific requirements.
User Input Handling:
• Responsive handling of mouse and keyboard input for player
interactions.
Graphics and Animation:
• Utilize SDL2 for rendering high-quality graphics and smooth
animations.
Multiplayer Mode:
• Enable two players to engage in a game simultaneously.

Screenshots of different states:

Main Menu of the Game


After pressing ‘1’ (Go to Chess Main Menu):

After Pressing ‘1’ (Enter Human vs Human Chess) :


Intermediate States:

CheckMate:
After pressing ‘2’ in CHESS MENU (AI PART):

First Page in TicTacToe (Press ‘2’ in main menu):


Intermediate States of TicTacToe Game:

Winners Message (Double Player):


Winners Message (Single Player):

CREDIT PAGE
Project Modules:
The project is diveded into three major modules-

• Singleplayer for Tic Tac Toe


• Doubleplayer for Tic Tac Toe
• Human vs Human for chess

Some header files are related with only one of these


modules,some for all the modules. A brief description of all the
header files is given below.

HEADER FILES, FUNCTIONS DETAILS:

tiktakshape.h:

Function in this header file:

drawCircle(): The drawCircle function uses a basic approach of


iterating through points within the bounding square of the circle and
checking if they are inside the circle using the circle equation.

drawCross(): The drawCross function draws a thick cross using


SDL's SDL_RenderDrawLine function. It considers the thickness
parameter to draw lines above and below the center point, creating
a thicker cross.

tiktakrander.h:

Function in this header file:


renderStartScreen(): This function is responsible for displaying the
initial screen when the game starts. It can include options such as
starting the game, exiting, or any other relevant choices.

renderGameScreen(): This function is responsible for renderingthe


main gameplay screen. It could display the game board, player
turns, and other relevant information during the active phase of the
game.

renderScoreScreen(): This function is responsible for rendering


the screen that appears after the game is over. It could display
the final score, the winner, and provide options for restarting or
quitting the game.

renderGame(): This function acts as a coordinator, determining


which specific rendering function to call based on the current game
state. It helps manage the transition between different screens in
the game.

tiktakevent.h:

Function in this header file:

handleEvent(): This function is responsible for handling general


events during the game, such as user inputs. It takes an
SDL_Event pointer as input, representing the event that occurred,
and an array (arr[]) representing the game state. The function
processes the event and updates the game state accordingly. It
returns a boolean value indicating whether the event was
successfully handled.

handleStartScreenEvent(): Specifically designed for the start


screen, this function handles events relevant to this phase of the
game. It takes an SDL_Event pointer and the game state array as
parameters. The function processes user inputs on the start screen,
potentially updating the game state with choices like starting the
game or quitting. It returns a boolean value indicatingwhether the
event was successfully handled.

handleGameScreenEvent(): Tailored for the main game screen,


this function handles events during active gameplay. It takes an
SDL_Event pointer and the game state array as parameters. The
function processes user inputs during the game, possibly
updating the game state with player moves. It returns a boolean
value indicating whether the event was successfully handled.

handleScoreScreenEvent(): Focused on the score or result screen,


this function handles events during the post-game phase. It takes
an SDL_Event pointer and the game state array as parameters.
The function processes user inputs on the score screen,
potentially updating the game state with actions like restarting or
quitting. It returns a boolean value indicating whether the event was
successfully handled.

computerTurn(): This function represents the computer's turn in the


game. It takes the game state array as a parameter. The function is
responsible for generating the computer's move basedon the
current game state. The return value is an integer representing the
computer's decision, such as a position on the game board. This
function is likely used in single-player games or when an AI
opponent is present.

starter.h:
Function in this header file:

initSDL(): The initSDL function is typically called at the beginning of


a program to set up the SDL environment, create a window, and
initialize the rendering system, enabling further graphics and
multimedia operations. Remember to properly handle error casesin
a complete implementation. Additionally, it seems like there are
global variables (gwindow and grenderer) used, so ensure they are
declared appropriately in the broader context of your program.

Starter(): In this function the cleanup code is repeated multiple


times.

game.h:

Function in this header file:

start(): This start function essentially represents the main game


loop, where events are handled, the game state is updated, and
the frame is rendered. The frame rate is controlled to prevent the
loop from running too fast, ensuring a more consistent frame rate
and smoother gameplay.

handleEvents(): This function captures input events, updating


relevant state variables that can be used in other parts of the
game logic. It's an essential part of the main game loop, ensuring
that user input is appropriately processed during each iteration.

update(): In summary, the update function appears to manage


the updating of the game state based on mouse input. It handles
the movement of pieces during a valid move and snaps a piece
into place when the mouse button is released after a valid move.
The specifics of the movePiece and snapPiece methods would
provide more insight into the exact behavior of these actions.

render(): In summary, the render function is responsible for clearing


the screen, rendering the chessboard, rendering the chess pieces,
and updating the screen to display the current state of the game.
The specific rendering details would depend on the implementation
of the render methods in the ChessBoard and PieceManager
classes.

initializeSDL(): The initializeSDL function is called during the


construction of the Game object and is responsible for setting up
the SDL environment, creating a window, and initializing a renderer
for graphics operations. The SDL functions used here areessential
for creating a graphical user interface in SDL-based applications.
The comments within the code suggest additional debugging
information that can be uncommented for debugging purposes.

credit.h:

Function in this header file:

initial(): The purpose of this function is preparing the SDL


environment and necessary resources for displaying a "Credit"
screen in an SDL-based application.

creditpagesec(): The function essentially displays a "Credit" page,


waits for the user to press the '1' key to exit, and handles SDL
events accordingly. It ensures proper cleanup of resources upon
exit. The exact rendering details and behavior of the credit page
depend on the implementation of the creditpagestarts function,which
is assumed to handle rendering the initial credit page.

copyofMain.h:

Function in this header file:

callFunction(): The callFunction function is a central control


mechanism for managing the execution of different games (Tic
Tac Toe and Chess) as well as navigating to a credit page in an
SDL-based application.

chessmain.h:

Function in this header file:

startHumanVsHuman(): This function is a concise entry point for


starting a human-vs-human game using an instance of the Game
class. The actual implementation of the start method within the
Game class contain the logic for managing the game loop,
handling player moves, and updating the game state.

chessboard.h:

Function in this header file:

render(): In summary, the render function iterates over each tileof


the chessboard, sets the renderer's draw color based on the
color of the tile, and then renders a filled rectangle representing
the tile on the renderer. This method is a crucial part of rendering
the entire chessboard in a graphical application.

chess.h:

Function in this header file:

StartChess(): In summary, the startChess function initializes SDL,


enters a loop to handle events (such as key presses and window
closure), takes actions based on user input, redraws the chess
screen accordingly, and cleans up SDL resources when the loop
exits. The specific actions include starting a human-vs-human
chess game, calling the NotDone function, or returning to the
main menu.

piecemanager.h:

Function in this header file:

render(): The render function is responsible for visually rendering


the chess pieces on the game board. It iterates through a map of
chess pieces, each represented by a unique pair of coordinates
and a ChessPiece object. For non-empty pieces, determined by the
ChessToken attribute, the method utilizes a switch statement to
differentiate between different types of chess pieces (king, queen,
bishop, knight, rook, pawn). Depending on the type and the player
to whom the piece belongs (determined by playerID), the method
calls a drawSprite function to render the corresponding sprite on
the game board. This rendering process iscrucial for updating the
graphical representation of the chess
game, ensuring that each piece is visually displayed based on its
type and player ownership.

movePiece(): This movePiece function essentially facilitates the


visual movement of a selected chess piece on the game board. It
calculates the new position based on the user's interaction (mouse
movement) and updates the location of the selected piece in the
internal representation of the chess game. The specific visual
effect would be determined by subsequent rendering operations
that update the graphical representation of the chessboard.

snapPiece(): This function plays a crucial role in the gameplay by


ensuring that chess pieces move to valid positions, updating the
game state, and handling conditions like checkmate.

checkValidPickup(): The checkValidPickup function in the


PieceManager class serves to validate whether a player can pick
up a chess piece from a selected tile on the game board. It first
identifies the tile based on the pixel coordinates of the selected
point. If there is no chess piece present at the identified tile, the
method returns false, indicating that there is no valid pickup.
However, if a piece exists at the selected tile, the method checks
whether it is the player's turn to move. If it is the correct player's
turn, the method returns true, indicating that the pickup is valid. On
the other hand, if it is not the player's turn, the method returns false,
signifying an invalid pickup attempt. This function plays a crucial
role in enforcing the turn-based movement rules ofthe chess game,
allowing for proper interaction and adherence to the game's
mechanics.
SpriteSheetInit(): The SpriteSheetInit function in the PieceManager
class serves to initialize and configure the sprite sheet containing
chess piece images. It begins by loading the sprite sheet image,
"ChessSprites.png," converting it into a texture for rendering using
SDL functions. The method then retrieves the dimensions of the
sprite sheet and defines rectangles for individual sprites based on a
specified number of rows and columns. Each rectangle, representing
a distinct chess piece image, is stored in the sprites vector. This
vector acts as a repository of sprite information, allowing the game to
access andrender specific sprites corresponding to different types of
chess pieces during gameplay. Overall, SpriteSheetInit is crucial for
setting up the visual resources necessary for rendering chess
pieces in the game.

drawSprite(): In summary, the drawSprite function encapsulates the


rendering process for displaying a specific chess piece sprite onto
the game screen. It facilitates the rendering of various chess piece
images by specifying the source sprite from the sprite sheetand the
destination on the screen where the sprite should be displayed. This
function is instrumental in visually representing different chess
pieces during gameplay.

initializeValidMoves(): The initializeValidMoves function in the


PieceManager class plays a key role in establishing the initial set
of valid moves for each chess piece in the game. Through iteration
over the pieces stored in the pieces map, the method examines the
type of each piece using a switch statement. For pawns, it
populates their valid moves based on the player's ID, allowing
forward movements to specific rows. Meanwhile, for knights, the
valid moves are determined by creating an L-shaped
pattern around the piece's location. This process ensures that each
piece, upon initialization or after a move, has an accurate
representation of the positions it can legally occupy on the
chessboard. By maintaining the validMoves set for each piece, the
method contributes to enforcing the rules of chess and facilitating
subsequent move validations during gameplay.

recomputeValidMoves(): The recomputeValidMoves function in the


PieceManager class is responsible for recalculating the valid
moves for each chess piece on the board, taking into account the
current game state. It iterates through the pieces stored in the
pieces map and, based on the type of each piece, updates its set
of valid moves. For example, it handles the movement patterns of
pawns, knights, bishops, rooks, queens, and kings, adjusting the
valid moves accordingly. Additionally, it checks for king safety by
examining potential moves and removing those that would put
the player's king in check. The method plays a crucial role in
ensuring that the chess pieces' valid moves are always up-to-
date, allowing for accurate move validation during gameplay and
contributing to the enforcement of chess rules.

kingSafe(): This function is designed to assess the safety of king


after a simulated chess move. The function creates a copy of the
current chessboard state and updates it to reflect the proposed
move. Subsequently, it iterates through all pieces on the board,
checking each opponent piece's ability to threaten the player's
king. The logic for different piece types, including pawns, knights,
bishops, rooks, queens, and kings, comprehensively evaluates
potential threats in various directions. While the function exhibitsa
solid approach to simulating and analyzing potential moves, there
is room for improvement in terms of code readability, duplication
reduction, and adherence to coding conventions.
Addressing these aspects would enhance the overall clarity,
maintainability, and reliability of the chess program.

checkmateChecker(): This function determines whether the current


board state represents a checkmate. It iterates through allpieces on
the board, checking if any of them have valid moves. If at least one
piece has valid moves, the function returns false, indicating that the
game is not in a checkmate state. On the otherhand, if no pieces
have valid moves, the function returns true, signaling a checkmate.
While the implementation is concise, its accuracy relies on the
correctness of the validMoves data stored for each piece, and it
assumes that a lack of valid moves for all pieces is a sufficient
condition for checkmate. It's worth noting that checkmate conditions
involve additional considerations, such as the ability to protect the
king, which might require further refinement of the algorithm for a
more comprehensive checkmatedetection.

checkmatePage.h:

Function in this header file:

Checks(): The function initializes SDL, creates a window, and


continuously renders a "Checkmate" text.
Teammate’s Responsibilities:
Our project has two parts, TicTacToe and Chess. We presented
TicTacToe in our project demo presentation and after that we
started our chess part. The first part (TicTacToe) was Abu Horaira’s
(26) responsibility also the window systems of the chessand
control flow was done by Horaira. Chess part was implemented
using some source codes from github by Adib(60) and Avi(44)
because some implementation was tough like King safety,
movement of some pieces and we are not so expert in C+
+ language. At first we tried to implement it using C but it shows
many errors and many complexity. That’s why we needed some
source code for chess.

Platform,Library&Tools:
• SDL2 for graphical interface
• VS code for code editor
• C and C++ as programming language
• Github for getting ideas

Limitations:
Throughout the whole project, we found few errors. In the portionof
chess we were unable to produce an AI so that we couldn’t playthe
single player mode. There were also some error in that part like
castling and promotion of pawn is not possible.
Conclusion:
In conclusion, the SDL2-based tic tac toe and chess game project
has successfully achieved its objectives of providing an engaging,
visually appealing, and functional digital rendition of the classic board
game. The utilization of SDL2 libraries has proven instrumental in
creating a robust and dynamic gaming environment. This project
not only serves as a testament to the capabilities of SDL2 but also
as a stepping stone for future developments and enhancements in
the realm of digital chess gaming.

Future Plan:
In our project there is no successfully implemented AI, in chess we
didn’t applied AI and in tic tac toe our AI is giving wrong result
sometimes. We might add AI in later. Furthermore, we will add
timer in chess and tic tac toe and save options in them.

Repositories and Drive Link of Video:


1. Github
2. Game Drive Link
3. YouTubeLink
References:
[1] The SDL Library.

https://wiki.libsdl.org/SDL2/FrontPage

[2] The SDL Wiki.

https://wiki.libsdl.org/SDL_image/FrontPage

[3] YouTube

https://www.youtube.com/

[4] Github 2D Games

https://github.com/topics/2d-game/

[5] ChatGPT

https://chat.openai.com

You might also like