jus to let everyone know, trying to program chutes and ladders sucks! im about to kill someone! if anyone would like to put me out of my misery and give me c++ code for chutes and ladders i will b your servant for life! *end of rant*
jus to let everyone know, trying to program chutes and ladders sucks! im about to kill someone! if anyone would like to put me out of my misery and give me c++ code for chutes and ladders i will b your servant for life! *end of rant*
Try programming it yourself, it's a learning experience. If have a specific problem then feel free to post that, not a request for the entire code.Don't expect someone to write your entire program.
I dont even know whats chutes and ladders so i search on google and i got this.
http://images.amazon.com/images/P/B0...S.LZZZZZZZ.gif
Is that what you want to program? Oh man if that is so, no one will code that for you. Why dont you give it an attempt and see what you come up with, or read some more. A lot of people think that within 3-4 months they will be able to make games and such. Then they try to make a game and are totally lost. Make small goals you can achieve, getting your hopes very high at the beginning is only a recipe for quiting.
When no one helps you out. Call google();
yeah that was jus my in class rant for the day. its not my fault that my goal has been set to friday. i didnt really expect people to do it. however i will post my entire code. if anyone has spare time let me know what you think. right now im stuck on getting the players to be displayed on the board.
eventually i want the return 0; in the runGame to return the number of the player that wins, i think i can get that tho. and the random cin >> play is meaningless other then putting a pause in the loop so it aloows the user to see wahats going on. if you run it just put in any number. if someone can jus get my players on the board i think i can take it from there. thanksCode://chutes and ladders //this will be my chutes and ladders program woo good fun! #include <iostream.h> #include <string.h> #include <iomanip.h> #include <stdlib.h> #include <time.h> const int ROWS=11; const int COLS=11; void createBoard(int board [ROWS] [COLS], int &r, int &c, int&count); void displayBoard(int board [ROWS] [COLS], int &r, int &c, int&count); void rules(); void playerSetup(char &player1, char &player2, char &player3, char &player4, int &numPlayers); int runGame(int board[ROWS][COLS], int r, int c, int count, char player1[31], char player2[31], char player3[31], char player4[31], int numPlayers); main() { char player1[31]; char player2[31]; char player3[31]; char player4[31]; int numPlayers=0; srand((unsigned)time(0)); int r=0, c=0, count=100; int board [ROWS][COLS]; rules(); createBoard(board, r, c, count); runGame(board, r, c, count, player1, player2, player3, player4, numPlayers); displayBoard(board, r, c, count); return 0; } void createBoard(int board[ROWS][COLS],int &r, int &c, int&count) { for (r=1; r<=10; r++) { if (r%2==1) { for (c=1; c<=10; c++) { board [r][c]=count--; } } if (r%2==0) { for (c=10; c>=1; c--) { board [r][c]=count--; } } } } void displayBoard(int board [ROWS] [COLS], int &r, int &c, int&count) { for (r=1; r<=10; r++) { for (c=1; c<=10; c++) { cout << setw(4) << board [r][c]; } cout << '\n' << '\n'; } } void rules() { cout << "We all played this game as a kid so i wont go too indepth into the rules.\n"; cout << "Roll the dice and move your piece. Your piece is represented by the first\n"; cout << "letter of the color you were given. A 'C' is the beginning of a chute, the end\n"; cout << "isnt shown. A 'L' is the beginning of a ladder, again the end isnt shown.\n"; cout << "First person to land on 100 wins. I'd wish you good luck but seeing as i \n"; cout << "dont like you i wish you nothing!\n\n\n"; } void playerSetup(char player1[31], char player2[31], char player3[31], char player4[31], int &numPlayers) { //player function int playersDone; do { cout << "How many players are there? (more then 1, no more then 4) "; cin >> numPlayers; cin.ignore(80, '\n'); }while(numPlayers>4 || numPlayers<=1); for (playersDone=1; playersDone<=numPlayers; playersDone++) { if (playersDone==1) { cout << "What is the name of player 1? "; cin.get(player1,30); cin.ignore(80,'\n'); cout << player1 << " will be a 111\n"; } if (playersDone==2) { cout << "What is the name of player 2? "; cin.get(player2,30); cin.ignore(80,'\n'); cout <<player2 << " will be a 222\n"; } if (playersDone==3) { cout << "What is the name of player 3? "; cin.get(player3,30); cin.ignore(80,'\n'); cout <<player3 << " will be a 333\n"; } if (playersDone==4) { cout << "What is the name of player 4? "; cin.get(player4,30); cin.ignore(80,'\n'); cout <<player4 << " will be a 444\n"; } } } int runGame(int board[ROWS][COLS], int r, int c, int count, char player1[31], char player2[31], char player3[31], char player4[31], int numPlayers) { int diceRoll; int gameDone=0, playerUp=1, mov1=0, mov2=0, mov3=0, mov4=0, play; playerSetup(player1, player2, player3, player4, numPlayers); cout << "Players will go in the order their names were entered.\n"; cout << player1 << player2 << player3 << player4 <<'\n'; while(gameDone!=1) { while(mov1<100 || mov2<100 || mov3<100 || mov4<100) { playerUp=1; cin >> play; cin.ignore(80, '\n'); if (playerUp==1) { diceRoll=(rand()%6)+1; cout << player1 << " rolled a " << diceRoll << '\n'; mov1=mov1+diceRoll; for (r=0; r<ROWS; r++) { for(c=0; c<COLS; c++) { if (count=mov1) { board[r][c]=111; } } } displayBoard(board, r, c, count); cin >> play; cin.ignore(80, '\n'); if (playerUp<numPlayers) playerUp++; } if (playerUp==2) { diceRoll=(rand()%6)+1; cout << player2 << " rolled a " << diceRoll << '\n'; mov2=mov2+diceRoll; for (r=0; r<ROWS; r++) { for(c=0; c<COLS; c++) { if (board[r][c]=mov2) { board[r][c]=222; } } } displayBoard(board, r, c, count); cin >> play; cin.ignore(80, '\n'); if (playerUp<numPlayers) playerUp++; } if (playerUp==3) { diceRoll=(rand()%6)+1; cout << player3 << " rolled a " << diceRoll << '\n'; mov3=mov3+diceRoll; for (r=0; r<ROWS; r++) { for(c=0; c<COLS; c++) { if (board[r][c]=mov3) { board[r][c]=333; } } } displayBoard(board, r, c, count); cin >> play; cin.ignore(80, '\n'); if (playerUp<numPlayers) playerUp++; } if (playerUp==4) { diceRoll=(rand()%6)+1; cout << player4 << " rolled a " << diceRoll << '\n'; mov4=mov4+diceRoll; for (r=0; r<ROWS; r++) { for(c=0; c<COLS; c++) { if (board[r][c]=mov4) { board[r][c]=444; } } } displayBoard(board, r, c, count); if (playerUp<numPlayers) playerUp++; } } } return 0; }
Last edited by ckeener; 03-15-2005 at 08:25 AM.
Since the board is a 2D array of int the players, chutes, and free board cells will need to be identified by a unique int values. For example, in the following display a 6 is a free cell, and a 7 is a chute. The players are indentified as 1, 2, 3, and 4.
6661766
3647766
2766667
6667666
If you want to use int values larger than a single digit as identifiers, then you will probably need to use formatted output to place the values in a neat visual display. What happens to a player if they land on a cell that's a chute is up to you.
EDIT: After rereading your post and looking at your code I see that there may be another way to interpret your question. That is, how to place the player on the board if all you know is the cumulative number of cells that they have moved, eg which row and col is palyer three at if player three has a move3 of 10 on a board that is x rows deep and y columns wide? Since rows and columns are indentified as indexes of an array their values range from 0 to x - 1 and 0 to y - 1, respectively. player3 would be in row index = move3 / row and col index = (move3 - 1) % y. So:
if move3 = 10;
and
int rows = x;
int cols = y;
then
int r = move3 / rows;
int c = (move3 - 1) % cols;
and locate player three on board by assigning the identifier for player three to the board cell with indicies r and c.
board[r][c] = 333;
Last edited by elad; 03-15-2005 at 10:20 AM.
You're only born perfect.
i quit, this program has defeated me. i give up, i dont care anymore! everything i try doesnt work. im through posting about it on here because everytime i geta reply i get excited that im finally goin to get it and then i just fail. maybe ill just flip burgers for a living. bahh
that's okay... with an attitude like that you weren't made out for a programming job anyway.Originally Posted by ckeener
With coding, as with everything in life, you need to modularize. not only your code, but your algorithm... in more simplistic terms, break it down. look at what you need to do before you try to do it:
- Pick a random number (spin the wheel)
- move that random amount of times
- depending on where you end up, do something
now you break it up even further, for example, let's take that last bullet:
- if you land on an empty space, do nothing
- if you end up on a ladder, go to to the space it points to
- if you end up on a chute, go to the space it points to.
now we turn those steps into pseudocode :
- if the space contains zero (0), go nowhere
- if the space contains a negative number, acknowledge that it's a chute (print something to the screen), and go backwards that amount of spaces, wrapping to the next line if necessary
- if the space contains a positive number, acknowledge that it's a ladder (print something to the screen), and go forwards that amount of spaces, wrapping to the next line if necessary
Last edited by major_small; 03-16-2005 at 09:11 AM.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Y I think you're being a bit hard on yourself. Learning how to step back and reassess the situation, ask for appropriate assistance etc. will serve you well whatever you end up doing. Think of coding as another practice in life skills if you don't think you'll end up doing this long term (for fun or profit). As it stands you don't give people much to help you with, so I'll just give it my best guess and leave it up to you whether you wish to offer more information as to your current problem or not.
Here's a sample as to how I would change your current code to encorporate my earlier suggestions. I've taken the liberty of using single digit int identifiers to make the job a little easier to start with.
Now let's say that player3 started at move3 = 7 and bord looked like this (first two rows of an 11 by 11 board), where 6 is an empty space on the board---that is no chutes and no players there.Code:int rowIndex, colIndex; if (playerUp==3) { diceRoll=(rand()%6)+1; cout << player3 << " rolled a " << diceRoll << '\n'; mov3=mov3 + diceRoll; rowIndex = move3/ROWS; colIndex = (move3 - 1) % COLS; board[rowIndex][colIndex] = 3; displayBoard(board, r, c, count); }
66666636666
66666666666
Now let's say diceRoll comes up 6 so move3 is incremented to 13. That puts rowIndex at 13/11 = 1 and colIndex at (13 - 1) = 12 % 11 = 1 so assign 3 to appropriate cell in board:
board[rowIndex][colIndex] = 3;
and we want displayBoard() to create something like this.
66666636666
63666666666
OH OH, we really want to change the initial position back to six when we move player3 to move3 = 13 but we have already changed move3 to the new value. So we need to go back and either keep the initial rowIndex and colIndex in variables we can use at this point, or change the initial cell value before we change the new cell.
66666666666Code:if (playerUp==3) { //change intial cell where palyer3 located rowIndex = move3/ROWS; colIndex = (move3 - 1) % COLS; board[rowIndex][colIndex] = 6; //roll die and change move3 accordingly diceRoll=(rand()%6)+1; cout << player3 << " rolled a " << diceRoll << '\n'; mov3=mov3 + diceRoll; //place player3 in new position rowIndex = move3/ROWS; colIndex = (move3 - 1) % COLS; board[rowIndex][colIndex] = 3; //now display board displayBoard(board, r, c, count); }
63666666666
There, that's better. Now we've accomplished a single move by player3. It's not the whole shebang, but it's a start. I recommend you change your displayBoard() function a bit:
This technique requires passing the const ints ROWS and COLS to many different functions in your program, and to me, this is the exception to the don't use global variables rule.Code:void displayBoard(int board [ROWS] [COLS], int ROWS, int COLS) { int r; int c; for (r = 0; r < ROWS; r++) { for (c =0; c < COLS; c++) cout << setw(4) << board [r][c]; cout << endl << endl; } }
Note also that you'll save yourself a lot of grief if you always use indexes ranging from 0 to size of array - 1 when using arrays. Do the conversions necessary to get to the index values or non-index position number as desired (index 0 is first element, index 1 is second element, etc.)
You're only born perfect.
i have that code in already, with the exception of the changes to the display board function i made those changes yesterday. it still doesnt work, i get no changes to my board. its exactly what u have, copy and paste. dont waste anymoe brain power on this. thanks for all your help but this thing is due tomorrow and its jus a lost cause. even if i would get them on the board i still have to put in so much stuff so im just stopping now.
here, I did it for you. you better look it over and learn it because I'm pretty sure there's probably some code in there you won't be able to explain to your teacher if you cant get this done on your own. I even stooped to using global variables for this one...
edit: I didn't bother reading your assignment, because I don't want to do it exactly the way it needs to be done. here's a very dirty basic engine you can use to get the game going. feel free to chop it up and use bits and pieces, or just to add your own code to it (you'll probably have to).
edit 2: sometimes it seems to go into an infinite loop--it's not--either the board is drawn in such a way that the first 5 spaces are all chutes, or you're just a very unlucky person and you just can't get the space(s) that are empty/ladders.
also, I did no checking in initializing the board--chutes may lead to ladders, and ladders may lead to chutes. I did, however, make sure there were a good amount of empty spaces.
edit 3: OT'ers--The only reason this code was posted is so we dont' have a suicide on our hands. I think we all know what it's like to have an assignment due in two hours that we can't figure out. Just know that I WILL NOT be giving ckeener a free ride through his C++ class.
Code:#include<iostream> #include<ctime> int board[10][10]; struct { int x; int y; } location; int move(int spaces); int spin(); int main() { srand(static_cast<unsigned int>(time(0))); location.x=0; location.y=0; int spaces=0; for(register int x=0;x<10;x++) { for(register int y=0;y<10;y++) { board[x][y]=5-rand()%21; board[x][y]=(board[x][y]>5||board[x][y]<-5?board[x][y]=0:board[x][y]); } } while(location.x<9 && location.y<9) { spaces=(spaces==0?spin():spaces); spaces=move(spaces); } std::cout<<"You Win"; std::cin.get(); return 0; } int move(int spaces) { while(spaces>0) { ++location.x; if(location.x>9) { location.x=0; ++location.y; location.y=(location.y>9?location.y=9,location.x=9:location.y); } --spaces; } while(spaces<0) { --location.x; if(location.x<0) { location.x=0; --location.y; location.y=(location.y<0?0:location.y); } ++spaces; } std::cout<<"You landed in space "<<location.x<<','<<location.y<<std::endl; std::cout<<"That space is "; if(board[location.x][location.y]<0) std::cout<<"a Chute, moving you back "<<board[location.x][location.y]<<" spaces"; else if(board[location.x][location.y]>0) std::cout<<"a Ladder, moving you up "<<board[location.x][location.y]<<" spaces"; else std::cout<<"empty."; std::cout<<std::endl; return board[location.x][location.y]; } int spin() { int move=1+rand()%5; std::cout<<"You Spin a "<<move<<std::endl; return move; }
Last edited by major_small; 03-17-2005 at 09:49 AM. Reason: see edits:
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Ya, copy and paste isn't the best approach. Too many times people with best intentions can provide code that isn't perfect as we don't compile and code, but intend to offer suggestions. Trying to understand what the code is trying to do and adapting it to your circumstances is a much better approach.
In looking over my code and trying further examples, I see that in calculating the rowIndex the formula should be rowIndex = (move3 - 1)/ROWS. Otherwise, when move3 is a multiple of ROWS it will place the player one row too high. move3/ROWS works for all positions but multilples of ROWS and I didn't check multiple of ROWS earlier.
I also see that you are trying to use indexes that range from 1-10 as a mechanism to deal with the array index starts with zero and the move3 starts with 1 problem. Unless your instructor tells you to do this, I think you are better off using the full 0 to size - 1 range.
Here's a stripped down program that just demonstrates the use of variables, technique to move a single player around the board, etc. You would need to expand the number of players, add ladders and chutes, make the display fancier if desired, add user input for player names, etc, etc to get it to bloom into the full project you had attempted. Frequently the technique of breaking down a bigger project into smaller ones that can be dealt with without all the distractions is very useful. Once you have the given process working, then try to add to it. This doesn't always work, but it works often enough to make it a very powerful trick to assist you in your project development. I have compiled and run this. It seems to work, but you'll want to confirm it.
Code:include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int ROWS=10; const int COLS=10; void createBoard(int board [ROWS] [COLS]); void displayBoard(int board [ROWS] [COLS]); void runGame(int board[ROWS][COLS]); main() { srand((unsigned)time(0)); int board [ROWS][COLS]; createBoard(board); runGame(board); return 0; } void createBoard(int board[ROWS][COLS]) { int r, c; //put the value 6 at every position of board for (r=0; r<ROWS; r++) for (c=0; c<COLS; c++) board [r][c]=6; } void displayBoard(int board [ROWS][COLS]) { int r, c; for (r=0; r<ROWS; r++) { for (c=0; c<COLS; c++) cout << board [r][c]; cout << endl; } } void runGame(int board[ROWS][COLS]) { int diceRoll; const int player1 = 1; int move1 = 0; int rowIndex, colIndex; displayBoard(board); cin.get(); while(move1<100) { //erase current position rowIndex = (move1 - 1)/ROWS; colIndex = (move1 - 1)%COLS; board[rowIndex][colIndex] = 6; //determine move diceRoll=(rand()%6)+1; cout << player1 << " rolled a " << diceRoll << endl; //advance players position move1=move1+diceRoll; cout << "move1 = " << move1 << endl; cin.get(); //place player1 at new position on board rowIndex = (move1 - 1)/ROWS; colIndex = (move1 - 1)%COLS; board[rowIndex][colIndex] = 1; displayBoard(board); cin.get(); } }
You're only born perfect.
I compile most of the working code I post on this board... and I ONLY post my own code, unless I'm working on somebody else's code... like my previous example--it runs.Originally Posted by eladneither he nor I used a 1..n indexing method. he used 1..n-1 and I used 0..n-1Originally Posted by eladbasically you just did the same thing I did in my previous post...Originally Posted by elad
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
And those who benefit from your (and others like you) should be very appreciative. However, I don't think anybody should just copy and paste code without understanding the principles behind it, particularly when they are in a learning situation. I don't take the time to proof all the code I post, so I don't gaurantee it's effectiveness. I do make a good faith effort and acknowledge my errors when they occur. Since this isn't an "ask the expert" site, I feel that my approach is as justifiable as yours, (thouh it may explain to some degree why my reputation is less than yours, too.)Originally Posted by major_small
fine. The point I was trying to make is that I feel it is better to use 0..n-1 than 1..n-1 where n is one more than the number of elements you will be using just so you can avoid the "artificialness" of starting indexing at 0.Originally Posted by major_small
Indeed. There are several reasons for that:Originally Posted by major_small
1) It is the natural flow of the program.
2) I generally compose my replies during slow times at work. That means it may be an hour or more between the time I start composing a reply and when I post. And if I am writing a program and proofing it, it may be even longer. Therefore, when I'm ready to post, my posts may already be outdated. If I notice that my post appears to have nothing to offer then I'll delete it. Otherwise, as in this instance, I'll leave it. This time I felt there was enough difference between what you were trying to do and my latest effort that I left it. If you feel that I stepped on your toes by doing so, I apologize. I assure you I did not post with that intention.
You're only born perfect.
Suicide because they couldn't program... Anyone know if someone actually did this?The only reason this code was posted is so we dont' have a suicide on our hands.
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com