Course code: EEE 2402
Course title: Structured Programming Language Laboratory
Submitted By :
Group: 2
1. Junayet Antor ( 0212320018 )
[Link] Islam Shifa (0212230061 )
[Link] Jubair ( 0212320028 )
Submitted To :
Md. Nure Alam Dipu
Lecturer, Department of Electrical and Electronic
Engineering
Introduction:
Our project was to make a game, where two players can compete with each other
to reach a final destination point. The players had the freedom to move their
character left, right, up and down. Their movement is limited by a dice that will
generate a random number from 1 to 6. First person to reach the wining grid will
be the winner and the code will end there.
How did we create the grid?
To create the grid we used symbols like +, - and |. The main idea was to create a
grid where players can easily understand their position.
This entire code is for the grid. Here, we can see that we have used a function
called “Grid” which is a void type function and the parameters are row, col, r1, r2,
c1, c2 which all are integer type. Here, the value of both row and column is 10
since the game will be in 10x10 grid. We can change that by changing the values
of row and col for example we can create 10x9 or 5x5 etc. Now, r1, and c1 are
positions of the first player. So r2 and c2 is for 2nd player.
For the grid itself we have used two “for” loops where “i” is controlling the row
and “j” is controlling the column. The outer for loop controls the number of row
and inside that we have used for loop that will repeat the pattern “+---” and “| “
to create a beautiful grid like this below.
And to print the position of the players we have used if conditions and based on
that the code will set the position of the players. This if condition is used in the
“|” part of the for loop since the position will be inside of the grid. And lastly we
have set the position of the wining grid constant, where the row is 4 and column
is 7 to make the game fair for both players.
How to move the position the player?
To move the player we have used “switch” statement. But before that we have to
take input from the user, in order to get the direction of the new position. To do
that we have used the below code.
Here, the code will ask the user for the direction and scanf function will take a
character type input and store the value in “D” which is a character type variable.
Then, in order to move the character depending on the direction we have used
the below code.
This is a simple switch function, here when a player gives input like “r” this means
right. So now the number of column will increase and then it will break. Similar
for the rest of the input we can change the position of the user. And if any player
decides to not to put anything then it will result in penalty and their turn will be
over.
This same code is again repeated for player 2. We could have used a function in
order to ease the work load but, this code also works just fine.
How did we set the border?
To set the border we had to use “if” conditions. Since the game will be in 10x10
therefore in code the range is from 0 to 9 for both row and column. So, any row
or column value less than 0 or negative number or greater than 9 will be invalid.
The below code is how we assigned a border.
As we can see, if the position of a player is less than 0 or greater than 9 then the
code will print invalid move and thus it will cause a penalty. Only if the new
position of the player is inside the grid then the code will print the grid with the
new position of the player.
How did we generate random number?
To create random number we have to use a function called rand() which is a
function of stdlib.h header file. And in order to generate a number within a range
we have used the formula below
Which in our case will be:
With the help of this we can set the range of the random number from 1 to 6. But
there is a problem, here the random number will generate the same number
every time we run the code, so in order to solve this problem we have used a
function srand(time(0)) which is a function of time.h header file. By using this we
can finally get random number.
Finally, how did we declare the winner?
declaring the winner is very simple, we just had to check the position of each
player after they assigned their new position.
If this condition is true, then the code will declare the player as winner break the
entire the code.
Key problems faced while making the code
1. The grid itself: we wanted to make a grid the will be both beautiful and
practical for a player. In order to do that we had to spend a huge time on it, the
whole code took us 8 hours to make, where the grid took 3 hours.
2. Code not taking direction: this problem was frustrating to say the least, the
code wasn’t taking any input from the user and kept crashing. But we didn’t know
where the problem was, so finding no other way we asked AI and he told us to
use a space in front of %c. The reason he said was that, the new line effect. We
don’t know much about it but when we put the space in front, the code worked
flawlessly.
3. Small quality of life changes: this problem included, if a player inputs an invalid
move to crash the code. It was fairly easy but hard to spot.
THAT IS ALL
THANK YOU SO MUCH