Hello everyone,
I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the dice to work in the way I want it to work.
I want the dice to work like this :
Before each player throw the dice they must start at 0.
Each player must throw a 6 on the dice to move on the board.
If a player threw a 6 on the dice, that player can throw again.
But I ended up with two seperate dice, one for each player (game is only for two players).And when I run the program, both players don't start at 0. And when I throw the dice, both players move at the same time but with different values.
If one of my players threw a 6, they just keep on throwing until someone wins the game. I tried to use a switch and if statements but I couldn't get it right.
And so I did this :
Code:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define SpaceBar 32// 32 is an ASCII value for a spacebar
//Prototypes
void TrowDice();
int dice, null, score=0;
int X, Y;
char Select;
int score,above, dice2, score2, above2;
int main(void)// main function
{
do
{
system("cls");// this is only to clear the screen and print the new values
ThrowDice();// the throw dice function
Select = getch();//select spacebar input if pressed, to roll the dice once
if(score==100)
{
printf("congratulaions Player 1 won the game");
return 0;
}
if(score2==100)
{
printf("congratulations Player 2 won the game");
return 0;
}
}
while(Select == SpaceBar);
}
void ThrowDice()//dice function
{
printf("the dice rolled = %d", dice);
printf("\nPlayer1 current position = %d\n",score);
srand(time(null));//to make the dice random
dice =(rand() % 6) + 1;//dice
score=score+dice;//total score of dice throws
if(score >100)//player must land on 100 exactly.
{
above = (score-100);
score = (100 - above);
}
switch(score)//this all my snakes and ladders for player 1
{
//snakes
case 28 :score=8;break;
case 57 :score=37;break;
case 81 :score=61;break;
case 91 :score=50;break;
case 97 :score=84;break;
//ladders
case 16 :score=25;break;
case 32 :score=52;break;
case 39 :score=79;break;
case 75 :score=86;break;
}//end switch
//This is my Player 2 section starting from here on.
printf("the dice rolled = %d", dice2);
printf("\nPlayer2 current position = %d\n",score2);
srand(time(null));// to make dice random
dice2 =(rand() % 6) + 1;// diece
score2=score2+dice2;// total dice throws for player 2
if(score2 >100)// must land on 100 exactly
{
above2 = (score2-100);
score2 = (100 - above2);
}
switch(score2)// all the snakes and ladders for player 2
{
//snakes
case 28 :score2=8;break;
case 57 :score2=37;break;
case 81 :score2=61;break;
case 91 :score2=50;break;
case 97 :score2=84;break;
//ladders
case 16 :score2=25;break;
case 32 :score2=52;break;
case 39 :score2=79;break;
case 75 :score2=86;break;
}
}
Can someone please tell me what I'm doing wrong or give me hints on what to do.
Thank you.