|
|
发表于 11-10-2008 08:50 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-10-2008 09:49 AM
|
显示全部楼层
噢,原来是逻辑上的问题啊,真是的,我以为电脑跟我闹别扭
做到蒙擦擦了。。。。。。
我又为我的program作了一些改进, 就是说如果其中一个player quit game了,其他player还可以continue玩,
大致上已经完成了, 只剩下一行还没解决, 就是
next=x+1;
if (next>=n)
next=0;
cout<<"\n\n\n\n"<<name[(next)]<<"'s turn!!!\n\n\n\n\n\n";
大大有什么建议吗?
[ 本帖最后由 Wongkokchoy 于 11-10-2008 10:25 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-10-2008 10:26 AM
|
显示全部楼层
- #include <iostream.h> // header file
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include <ctype.h>
- void Line(); // function phototypes
- void Ladder(char[]);
- void Snake(char[]);
- void wall();
- void winner(int, char[]);
- void main()
- {
- char dice, **name; // declaration of variable
- int A, n, number, next, x, reverse, *player, *cont;
- Line(); // function call
-
- cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n"; // instuction
- cout<<"Introduction: if a player reach a point where there is a ladder, he/she will \nraise to a higher location.\n";
- cout<<"\nOn the other hand, if the player reach a spot where there is a snake over there,the player will drop to a lower location.\n\n";
- cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
- Line(); // function call
- do // do-while loop to avoid user to key in negative value or zero
- {
- cout<<"\nPlease enter the numbers of players:\n";
- cin>>n;
- cin.get();
- if (n<=0)
- cout<<"INVALID number of player!\n";
- }
- while (n<=0);
- player=new int [n]; // declare array "player" and "name"
- name=new char* [n];
- cont=new int [n];
- for(int i = 0; i < n ;i++)
- name = new char[100];
- for (int x=0; x
- {
- cout<<"lease enter player "<<(x+1)<<"'s name.\n"; //records players' name
- cin.getline (name[x], 100);
- }
- for (int x=0; x
- {
- cout<<"layer "<<(x+1)<<"'s name is "<<<".\n";
- }
- cout<<"\nSo there will be "<<<" player(s) in the game.\n";
- cout<<"There are ladders at 4, 9, 19, 21, 28, 51, 71. 80 \n(80 will directly send you to the vitory!!)\n";
- cout<<"Snakes located at 17, 54, 62, 64, 87, 93, 96, 98.\n\n";
- cout<<"(All players start at 0)\n\n\n";
- cout<<"Good Luck and Have Fun!!!\n\n";
- Line(); // funciton call
- for (x = 0; x < n; x++) // sets all cell values in the array player to 0
- {
- player[x] = 0;
- cont[x]=0;
- }
- do
- {
- for (x=0; x
- {
- if (cont[x]==0)
- {
- number=x+1;
- cout<<"\n\n\n\n\n\n"<<<" is now at position "<<<".\n"; // tell players their position and snake and ladders' location
- cout<<"Ladders (4, 9, 19, 21, 28, 51, 71. 80)\n";
- cout<<"Snakes (17, 54, 62, 64, 87, 93, 96, 98)\n\n";
- srand(time(0)); // seeds the random sequence generated by rand() later
- do // a do-while loop to ensure that the choice entered by users is either t or q
- {
- cout<<"ress 'T' to throw a dice!\nPress 'Q' to quit game.\n";
- cin>>dice;
- if (toupper(dice) !='Q' && toupper(dice) !='T') // toupper is used so that it is exercuate without
- // considering the upper and lower case of the choice
- {
- Line(); // funciton call
- cout<<"\nINVALID CHOICE.\n\n";
- Line(); // funciton call
- }
- }
- while (toupper(dice)!='Q'&& toupper(dice)!='T');
- if (toupper(dice)=='Q')
- {
- cout<<"\n\n\n"<<<" had quit the game."; // exits from the loop when user key in 'q' or 'Q'
- cont[x]=1;
- cin.get();
- cin.get();
- break;
- }
- else if (toupper(dice)=='T')
- {
- A=(rand()%6)+1; // randomly generate a number from 1 to 6
- if (A==1)
- cout<<"\n \n . \n \n\n\n"<<<" gets 1!!\n"; // displays to tell users the number on the 'dice'
- else if (A==2)
- cout<<"\n . \n \n . \n\n\n"<<<" gets 2!!\n";
- else if (A==3)
- cout<<"\n. \n . \n .\n\n\n"<<<" gets 3!!\n";
- else if (A==4)
- cout<<"\n. .\n \n. .\n\n\n"<<<" gets 4!!\n";
- else if (A==5)
- cout<<"\n. .\n . \n. .\n\n\n"<<<" gets 5!!\n";
- else if (A==6)
- cout<<"\n. .\n. .\n. .\n\n\n"<<<" gets 6!!\n";
- player[x]+=A;
- if( player[x] > 100 ) // when users' position is over 100, user will reverse to their respective position
- {
- reverse = player[x]-100;
- wall(); // funciton call
- cin.get();
- cout<<" "<<<" had reached "<<<"!\n\n";
- cin.get();
- player[x] = 100 - ( player[x] - 100 );
- cout<<" Ops!! "<<<" knocks the wall!\nPlayer "<<<" will reverse "<<<" steps......\n\n";
- wall(); // funciton call
- cout<<""<<<" is now at position "<<<"!\n\n";
- cin.get();
- }
- switch (player[x]) // place the location of snakes and ladders
- {
- case 4:
- Ladder(name[x]); // funciton call
- player[x]=14;
- break;
- case 9:
- Ladder(name[x]); // funciton call
- player[x]=31;
- break;
- case 19:
- Ladder(name[x]); // funciton call
- player[x]=38;
- break;
- case 21:
- Ladder(name[x]); // funciton call
- player[x]=42;
- break;
- case 28:
- Ladder(name[x]); // funciton call
- player[x]=84;
- break;
- case 51:
- Ladder(name[x]); // funciton call
- player[x]=67;
- break;
- case 71:
- Ladder(name[x]); // funciton call
- player[x]=91;
- break;
- case 80:
- Ladder(name[x]); // funciton call
- player[x]=100;
- break;
- case 17:
- Snake(name[x]); // funciton call
- player[x]=7;
- break;
- case 62:
- Snake(name[x]); // funciton call
- player[x]=19;
- break;
- case 54:
- Snake(name[x]); // funciton call
- player[x]=34;
- break;
- case 64:
- Snake(name[x]); // funciton call
- player[x]=60;
- break;
- case 87:
- Snake(name[x]); // funciton call
- player[x]=24;
- break;
- case 93:
- Snake(name[x]); // funciton call
- player[x]=73;
- break;
- case 95:
- Snake(name[x]); // funciton call
- player[x]=75;
- break;
- case 98:
- Snake(name[x]); // funciton call
- player[x]=79;
- break;
- }
- cin.get();
- cout<<""<<name[x]<<" had reached "<<player[x]<<"!\n\n"; // displays usres' position
- cin.get();
- }
- if (player[x]==100) // exits loops when one of the players reach 100
- {
- winner (number, name[x]);
- break;
- }
- next=x+1;
- if (next>=n)
- next=0;
- cout<<"\n\n\n\n"<<<"'s turn!!!\n\n\n\n\n\n";
- }
- }
- }
- while(player[x]!=100); // keep the looping untill a player reach 100
- } // end of main funciton
- // functions definition
- void Line()
- {
- cout<<"-------------------------------------------------------------------------------\n";
- }
- void Ladder(char name[])
- {
- cin.get();
- cout<<"\n\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH_LADDER_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n\n";
- cout<<"\nLook!! There is a ladder over there!\n";
- cin.get();
- cout<<""<<<" climb up the ladder.\n";
- }
- void Snake(char name[])
- {
- cin.get();
- cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SNAKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
- cout<<"\nWoot!! "<<<" meets an anaconda!!!!\n";
- cin.get();
- cout<<"In order to escape from the snake,"<<<" had run away.\n";
- }
- void wall()
- {
- cout<<"\n\n`````````````````````````````````WALL``````````````````````````````````````````\n\n";
- }
- void winner(int number,char name[] )
- {
- cin.get();
- cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
- cout << "\nPlayer [" <<< "] - "<<<" win!!!!\n\nCongratulation!!!\n\n"<
- cout<<"----------------------------------WINNER---------------------------------------\n";
- }
- // end of functions definition
复制代码
[ 本帖最后由 Wongkokchoy 于 11-10-2008 01:19 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-10-2008 05:57 PM
|
显示全部楼层
还没有人来帮我吗。。。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 11-10-2008 08:05 PM
|
显示全部楼层
回复 144# Wongkokchoy 的帖子
只要加入一个 quit flag, 然后设定若 quit_flag == true, 就 skip, 否则继续
没法些个sample了, 这几天都要考试,很忙。 看看其他的大大能不能帮你吧 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-10-2008 09:07 PM
|
显示全部楼层
|
我想不到要怎样link next跟cont[x], 这个解决的话, 就完了 |
|
|
|
|
|
|
|
|
|
|
发表于 12-10-2008 08:51 PM
|
显示全部楼层
刚才花了些时间来做一个,
因为你的code 太长乐,
要修改有点复杂。
而且可以减少case 的应用。
贴出来参考参考,分享分享。
有错请给意见。
用BC++- #include <iostream>
- #include <conio>
- #include <math.h>
- #include <time.h>
- #include <stdlib.h>
- int check (int a);
- int calc_step(int player , int step)
- {
- int score = player + step;
- if (score ==100)
- return score;
- else if (score<100)
- player+=step;
- else if (score>100)
- {
- cout <<"Over ladder ... reverse !!! "<<endl;
- player = 200-score;
- }
- return check(player); //call 下面的function 来检查是否升降。
- }
- int check (int a)
- {
- int step[]={4, 9, 19, 21, 28, 51, 71,80 , 17, 54, 62, 64, 87, 93, 96, 98}; //代表升和降的位置,可以自己修改
- int goTo[]={14,31,38,42,84,67,91,100 ,7,34,19,60,24,73,75,79}; //应对上面的位置所变换到的位置
- for(int i=0 ; i <16 ; i++)
- if (a==step[i])
- {
- cout <<"******caution********* From "<<step[i]<<" goto "<<goTo[i]<<endl;
- return goTo[i] ;
- }
- return a; //如果没有升降,就return 原本的。这个return 会return 给calc_step ,然后calc_step return 回去main. 两个都要int , 因为这样你不用call 第二次function来检查是否升降。
- }
- int main(){
- int num;
- int count=0;
- bool win = false; //看是否输赢。
- int step;
- int winner ;
- cout <<"lease enter the number of player"<<endl;
- cin >>num;
- int * player = new int[num];
- char* name = new char [num];
- name = new char[20];
- for (int i=0; i < num; i++)
- {
- player[i]=0; //将所有分数置0
- name[i]=(i+80); // 因为只是测试版本,所以名字随便放
- }
- cout <<"press 't' to throw , 'q' to quit"<<endl;
- do{
- for(int i=0; i<num ; i++)
- {
- cout <<"player["<<(i+1)<<"] : name = "<<name<<" , score = "<< player[i]<<endl;
- if(getch()=='q') //getch() 相当于你的cin.get()
- return 0; //exit
- srand(time(0));
- step=(rand()%6)+1;
- cout <<"The dice is "<<step<<endl; //直接可以print出来不用用switch
- player[i] = calc_step(player[i] , step);
- if (player[i]==100)
- {
- win= true;
- winner = i;
- }
- cout <<"player["<<(i+1)<<"] : name = "<<name<<" , score = "<< player[i]<<endl<<endl;
- }
- }while(!win); //看是否输赢。
- cout <<"The winner is player["<<winner<<"] : , name : "<<name[winner];
- getch();
- return 0;
- }
复制代码
[ 本帖最后由 ss_sky87 于 12-10-2008 09:13 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-10-2008 09:40 PM
|
显示全部楼层
flow chart 已经画了,所以就算了吧。。。。
不过想问一下,C++ 能不能放图片作background? |
|
|
|
|
|
|
|
|
|
|
发表于 12-10-2008 10:09 PM
|
显示全部楼层
回复 143# Wongkokchoy 的帖子
|
呵呵, 可以重新贴过吗你的code。 上面那个有很多error噢。 无法帮你查看。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-10-2008 10:21 PM
|
显示全部楼层
- #include <iostream.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include <ctype.h>
- void Line();
- void Ladder(char[]);
- void Snake(char[]);
- void wall();
- void winner(int, char[]);
- void main()
- {
- char dice, **name;
- int A, n, number, x, reverse, *player, *cont;
- Line();
-
- cout<<"\t\tWelcome to Snake and Ladder Game!!\n\n";
- cout<<"Introduction: if a player reach a point where there is a ladder, he/she will \nraise to a higher location.\n";
- cout<<"\nOn the other hand, if the player reach a spot where there is a snake over there,the player will drop to a lower location.\n\n";
- cout<<"The 1st player who reach 100 first will be the WINNER!!!!!!\n";
- Line();
- do
- {
- cout<<"\nPlease enter the numbers of players:\n";
- cin>>n;
- cin.get();
- if (n<=0)
- cout<<"INVALID number of player!\n";
- }
- while (n<=0);
- player=new int [n];
- name=new char* [n];
- cont=new int [n];
- for(int i = 0; i < n ;i++)
- name = new char[100];
- for (int x=0; x<n; x++)
- {
- cout<<"lease enter player "<<(x+1)<<"'s name.\n";
- cin.getline (name[x], 100);
- }
- for (int x=0; x<n; x++)
- {
- cout<<"layer "<<(x+1)<<"'s name is "<<name[x]<<".\n";
- }
- cout<<"\nSo there will be "<<n<<" player(s) in the game.\n";
- cout<<"There are ladders at 4, 9, 19, 21, 28, 51, 71. 80 \n(80 will directly send you to the vitory!!)\n";
- cout<<"Snakes located at 17, 54, 62, 64, 87, 93, 96, 98.\n\n";
- cout<<"(All players start at 0)\n\n\n";
- cout<<"Good Luck and Have Fun!!!\n\n";
- Line();
- for (x = 0; x < n; x++)
- {
- player[x] = 0;
- cont[x]=0;
- }
- do
- {
- for (x=0; x<n; x++)
- {
- if (cont[x]==0)
- {
- number=x+1;
- cout<<"\n\n\n\n\n\n"<<name[x]<<" is now at position "<<player[x]<<".\n";
- cout<<"Ladders (4, 9, 19, 21, 28, 51, 71. 80)\n";
- cout<<"Snakes (17, 54, 62, 64, 87, 93, 96, 98)\n\n";
- srand(time(0));
- do
- {
- cout<<"ress 'T' to throw a dice!\nPress 'Q' to quit game.\n";
- cin>>dice;
- if (toupper(dice) !='Q' && toupper(dice) !='T')
-
- {
- Line();
- cout<<"\nINVALID CHOICE.\n\n";
- Line(); // funciton call
- }
- }
- while (toupper(dice)!='Q'&& toupper(dice)!='T');
- if (toupper(dice)=='Q')
- {
- cout<<"\n\n\n"<<name[x]<<" had quit the game.";
- cont[x]=1;
- cin.get();
- cin.get();
- break;
- }
- else if (toupper(dice)=='T')
- {
- A=(rand()%6)+1; \
- if (A==1)
- cout<<"\n \n . \n \n\n\n"<<name[x]<<" gets 1!!\n";
- else if (A==2)
- cout<<"\n . \n \n . \n\n\n"<<name[x]<<" gets 2!!\n";
- else if (A==3)
- cout<<"\n. \n . \n .\n\n\n"<<name[x]<<" gets 3!!\n";
- else if (A==4)
- cout<<"\n. .\n \n. .\n\n\n"<<name[x]<<" gets 4!!\n";
- else if (A==5)
- cout<<"\n. .\n . \n. .\n\n\n"<<name[x]<<" gets 5!!\n";
- else if (A==6)
- cout<<"\n. .\n. .\n. .\n\n\n"<<name[x]<<" gets 6!!\n";
- player[x]+=A;
- if( player[x] > 100 )
- {
- reverse = player[x]-100;
- wall(); // funciton call
- cin.get();
- cout<<" "<<name[x]<<" had reached "<<player[x]<<"!\n\n";
- cin.get();
- player[x] = 100 - ( player[x] - 100 );
- cout<<"Ops!! "<<name[x]<<" knocks the wall!\n"<<name[x]<<" will reverse "<<reverse<<" steps......\n\n";
- wall(); // funciton call
- cout<<""<<name[x]<<" is now at position "<<player[x]<<"!\n\n";
- cin.get();
- }
- switch (player[x])
- {
- case 4:
- Ladder(name[x]);
- player[x]=14;
- break;
- case 9:
- Ladder(name[x]);
- player[x]=31;
- break;
- case 19:
- Ladder(name[x]);
- player[x]=38;
- break;
- case 21:
- Ladder(name[x]); // funciton call
- player[x]=42;
- break;
- case 28:
- Ladder(name[x]);
- player[x]=84;
- break;
- case 51:
- Ladder(name[x]);
- player[x]=67;
- break;
- case 71:
- Ladder(name[x]);
- player[x]=91;
- break;
- case 80:
- Ladder(name[x]);
- player[x]=100;
- break;
- case 17:
- Snake(name[x]);
- player[x]=7;
- break;
- case 62:
- Snake(name[x]);
- player[x]=19;
- break;
- case 54:
- Snake(name[x]);
- player[x]=34;
- break;
- case 64:
- Snake(name[x]);
- player[x]=60;
- break;
- case 87:
- Snake(name[x]);
- player[x]=24;
- break;
- case 93:
- Snake(name[x]);
- player[x]=73;
- break;
- case 95:
- Snake(name[x]);
- player[x]=75;
- break;
- case 98:
- Snake(name[x]);
- player[x]=79;
- break;
- }
- cin.get();
- cout<<""<<name[x]<<" had reached "<<player[x]<<"!\n\n";
- cin.get();
- }
- if (player[x]==100)
- {
- winner (number, name[x]);
- break;
- }
- }
- cout<<"\n\n\n\nNext player!!!\n\n\n\n\n\n";
-
- }
- }
- while(player[x]!=100);
- }
- void Line()
- {
- cout<<"-------------------------------------------------------------------------------\n";
- }
- void Ladder(char name[])
- {
- cin.get();
- cout<<"\n\nHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH_LADDER_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n\n";
- cout<<"\nLook!! There is a ladder over there!\n";
- cin.get();
- cout<<""<<name<<" climbs up the ladder.\n";
- }
- void Snake(char name[])
- {
- cin.get();
- cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SNAKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
- cout<<"\nWoot!! "<<name<<" meets an anaconda!!!!\n";
- cin.get();
- cout<<"In order to escape from the snake,"<<name<<" had run away.\n";
- }
- void wall()
- {
- cout<<"\n\n`````````````````````````````````WALL``````````````````````````````````````````\n\n";
- }
- void winner(int x,char name[] )
- {
- cin.get();
- cout << "\nPlayer ["<<(x+1)<<"] - "<<name<<" win!!!!\n\nCongratulation!!!\n\n"<<endl;
- }
复制代码
[ 本帖最后由 Wongkokchoy 于 12-10-2008 10:45 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 12-10-2008 11:26 PM
|
显示全部楼层
回复 150# Wongkokchoy 的帖子
|
感觉上好像没什么问题除了第一个玩家赢利就跳出来外。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 13-10-2008 12:06 AM
|
显示全部楼层
对啊,已经没问题了,只是一个player赢了,就停了。 
现在只是想改进罢了。 队友问能不能够把整个board放进去C++。所以就来问问看咯。。。
[ 本帖最后由 Wongkokchoy 于 13-10-2008 12:08 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 14-10-2008 05:42 PM
|
显示全部楼层
再来一个, 如果user在 no. of player 那边 enter 1.2, 我的program就变成了 一个player, 那个player叫做2
有没有办法来check user enter的是不是整数? |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 01:19 AM
|
显示全部楼层
原帖由 Wongkokchoy 于 14-10-2008 05:42 PM 发表 
再来一个, 如果user在 no. of player 那边 enter 1.2, 我的program就变成了 一个player, 那个player叫做2
有没有办法来check user enter的是不是整数?
只想到if((x-ceil(x)) <0)
[ 本帖最后由 lliioonn 于 15-10-2008 01:32 AM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|