#include<iostream>
#include<conio.h>
#include<ctime>
#include<cstdlib>
#include<string>
int score=0;
using namespace std;
void display(int &score)
{ 

    switch(score)
    {
    case 98:score=28;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 95:score=24;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 92:score=51;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 83:score=19;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 73:score=1;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 69:score=33;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 64:score=36;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 59:score=17;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 55:score=7;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 52:score=11;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 48:score=9;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 46:score=5;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 44:score=22;cout<<"OPPSSSSS......snaked attacked you...now you are at "<<score<<endl;break;
    case 8:score=26;cout<<"Great work.........you are at "<<score<<endl;break;
    case 21:score=82;cout<<"Great work.........you are at "<<score<<endl;break;
    case 43:score=77;cout<<"Great work.........you are at "<<score<<endl;break;
    case 50:score=91;cout<<"Great work.........you are at "<<score<<endl;break;
    case 62:score=96;cout<<"Great work.........you are at "<<score<<endl;break;
    case 66:score=87;cout<<"Great work.........you are at "<<score<<endl;break;
    case 88:score=100;cout<<"Great work.........you are at "<<score<<endl;break;
    }}


int main()
{

    srand ( static_cast<unsigned int>(time(NULL)) );

    int choice;
    string name1,name2;
    int p1=0;
        int p2=0;

    cout<<"enter name of PLAYER 1  \n:";
        getline(cin,name1);
    cout<<"Enter name of PLAYER 2  \n:";
    getline(cin,name2);
    while((p1<=100)&&(p2<=100))
    {
    //system("cls");
    cout<<"----/\PLAYER1 your turn... \n " ;
    getch();
    int n=rand()%6+1;
    p1=n+p1;
    cout<<"player 1 is at "<<p1<<endl;
    display(p1);
    cout<<"----/\PLAYER2 your turn...press space bar \n " ;
    getch();
int m=rand()%6+1;
    p2=m+p2;
    cout<<"player 2 is at "<<p2<<endl;
    display(p2);
    }
if(p1>p2)
    cout<<name1<<"  wins....good work";
else
        cout<<name2<<"  wins....good work";


    system("pause");
    return 0;



    }

Dani AI

Generated

This program is a good start, but it needs three focused changes to behave like a real, maintainable Snake & Ladder game: correct turn/finish logic, clear separation of game logic vs UI, and a tidy way to store snakes/ladders so behavior is easy to change.

First, fix the end-of-game logic. Do not wait until the loop finishes and then compare positions — check immediately after each move and break when a player reaches the winning square (or implements the chosen “exact 100” rule). The current loop condition lets play continue and then decides the winner incorrectly.

Second, separate responsibilities. Keep pure game logic (roll dice, move, apply snake/ladder) apart from printing and input. Replace non-portable getch()/conio.h with standard input (for example, std::cin.get() or read a single char like 'r' to roll). Replace global variables with a Player struct or class holding name and position.

Third, store snakes and ladders in a data structure (map/unordered_map) instead of a long switch; that makes maintenance and testing easier. Use a modern RNG (std::mt19937) rather than repeated rand()/srand().

Small, safe code skeleton you can apply:

#include <random>
#include <unordered_map>

std::mt19937 rng{std::random_device{}()};
int rollDice() {
  static std::uniform_int_distribution<int> d(1,6);
  return d(rng);
}

void applyJump(int &pos, const std::unordered_map<int,int>& jumps) {
  auto it = jumps.find(pos);
  if (it != jumps.end()) pos = it->second;
}

Main-loop notes: prompt the current player to press Enter, call rollDice(), compute candidate = pos + roll, decide overshoot behavior (stay or bounce), update pos, call applyJump, then immediately check if (pos == 100) { declare winner; break; }. was right to flag the “machine playing itself” issue — add explicit player input to fix that. Also, , your later bonus program looks like a different problem; keep it in a separate thread so replies don’t get mixed.

Recommended Answers

All 3 Replies

This is just the machine playing with itself. What about real user input? This is a situation where a proper description of the flow of the game is required. Pseudo code is ok, or just text descriptions of how the game should flow, and how user input is interpreted to control the flow of the game. If this were a class assignment, I don't think you'd get much more than a D- (if I were feeling generous the day I graded your work).

actually i was asking about its logic.....

in this program bonus is not calculating.............why

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
using namespace std;
void display(int bonu[])
{
    ofstream outdata;
    outdata.open("employee.txt",ios::app);

    outdata<<"saleperson\t\t\tbonus\n";
    for(int i=0;i<10;i++)
    {
        outdata<<i<<"\t\t\t";
        outdata<<bonu[i]<<endl;

     }




}
void bon (int arra[][3],int bonus[])
{

for(int i=0;i<10;i++){
for(int j=0;j<3;j++)
{
if((arra[i][j]>=5001)&&(arra[i][j]<=10000))
    bonus[i]=bonus[i]+(arra[i][j]*0.15);
if((arra[i][j]>=1001)&&arra[i][j]<=5000)
    bonus[i]=bonus[i]+(arra[i][j]*0.20);
if((arra[i][j]>=501)&&arra[i][j]<=1000)
    bonus[i]=bonus[i]+(arra[i][j]*0.15);
if((arra[i][j]>=200)&&arra[i][j]<=500)
    bonus[i]=bonus[i]+(arra[i][j]*0.10);
else
    bonus[i]=bonus[i]+arra[i][j];
}
} 

display(bonus);



}


int main()
{
    srand(time('\0'));
    int n;
    ifstream infile;
    ofstream outdata;
    outdata.open("employee.txt",ios::app);
    outdata<<"january\t\t\tfebuary\t\t\tmarch\n";
    for(int i=0;i<10;i++)
    {
    for(int j=0;j<3;j++)
    {
     n=rand()%10000+200;
     outdata<<n<<"\t\t\t";
     }outdata<<endl;
    }
    outdata.close();
    infile.open("employee.txt");
    int bonus[10]={0};
    int arra[10][3]={(0,0)};
    for(int i=0;i<10;i++){
    for(int j=0;j<3;j++){
    infile>>arra[i][j];
     }}

    bon(arra,bonus);
    outdata.close();
    infile.close();
    system("pause");
     return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.