|
|

楼主 |
发表于 9-10-2008 08:34 PM
|
显示全部楼层
请问大大有谁会用goto loop 吗?
我朋友的code需要用到。(虽然很冒险)
他的code: |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-10-2008 08:38 PM
|
显示全部楼层
[ 本帖最后由 Wongkokchoy 于 9-10-2008 08:45 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 09:22 PM
|
显示全部楼层
回复 121# Wongkokchoy 的帖子
不明白为什么要用到goto, 无论如何, syntax 如下- #include <iostream>
- using namespace std;
- int main()
- {
- int input;
- cout << "goto : ";
- cin >> input;
- switch(input)
- {
- case 1:
- goto ab;
- case 2:
- goto ac;
- case 3:
- goto ad;
- case 4:
- goto af;
- }
- ab:
- cout << "ab" << endl;
- ac:
- cout << "ac" << endl;
- ad:
- cout << "ad" << endl;
- af:
- cout << "af" << endl;
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-10-2008 12:13 AM
|
显示全部楼层
我的朋友想让user key-in 'y' 之后,回去前面再run过,有想过要用do-while loop, 可是因为前面已经用了do-while loop, 两个do-while loop 混淆了
我的问题:
我现在的program是当其中一个player打'Q'的时候, 整个program就停了,所以我想改成就算某某player quit 了,其他的player还是可以continue玩下去。
想过放do-while, 可是因为之前已经有一个do-while了, 所以行不通。。。
有什么建议吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-10-2008 12:18 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;
- Line(); // function call
- <<endl;
- cout<<"*******************************************************************************"<<endl<<endl;
- 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];
- for(int i = 0; i < n ;i++)
- name[i] = new char[100];
- for (int x=0; x<n; x++)
- {
- cout<<"Please enter player "<<(x+1)<<"'s name.\n"; //records players' name
- cin.getline (name[x], 100);
- }
- for (int x=0; x<n; x++)
- {
- cout<<"Player "<<(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(); // funciton call
- for (x = 0; x < n; x++) // sets all cell values in the array player to 0
- player[x] = 0;
- do
- {
- for (x=0; x<n; x++) // save players' position into array "player"
- {
- number=x+1;
- cout<<"\n\n\n\n\n\n"<<name[x]<<" is now at position "<<player[x]<<".\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<<"Press '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\nUser had quit the game."; // exits from the loop when user key in 'q' or 'Q'
- 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"<<name[x]<<" gets 1!!\n"; // displays to tell users the number on the 'dice'
- 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 ) // when users' position is over 100, user will reverse to their respective position
- {
- 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<<" 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]) // 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();
- next = x+2;
- }
- if (player[x]==100) // exits loops when one of the players reach 100
- {
- winner (number, name[x]);
- break;
- }
- if (next==n+1)
- next=1;
- cout<<"\n\n\n\n"<<name[next]<<"'s turn!!!\n\n\n\n\n\n";
- }
- if (toupper(dice)=='Q')
- break;
- }
- 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<<""<<name<<" climb 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 number,char name[] )
- {
- cin.get();
- cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
- cout << "\nPlayer [" <<number<< "] - "<<name<<" win!!!!\n\nCongratulation!!!\n\n"<<endl;
- cout<<"----------------------------------WINNER---------------------------------------\n";
- }
- //end of functions definition
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-10-2008 03:21 PM
|
显示全部楼层
另一个无礼取闹的error:
- for (int x=0; x<n; x++)
- {
- cout<<"Please enter player "<<(x+1)<<"'s name.\n"; //records players' name
- cin.getline (name[x], 100);
- }
- for (int x=0; x<n; x++)
- {
- cout<<"Player "<<(x+1)<<"'s name is "<<name[x]<<".\n";
- }
- for (x=0; x<n; x++) // save players' position into array "player"next = number;
- {
- next=x+1;
- if (next==n+1)
- next=0;
- cout<<"\n\n\n\n"<<name[next]<<"'s turn!!!\n\n\n\n\n\n";
- }
复制代码
括弧那些应该没问题,第一次looping的时候没问题,可是要loop第二次的时候,就会出现
Thread stopped.
Fault: access violation at 0x407c01:write of address 0X18
之前的是
next = x+2;
if (next==n+1)
next=1;
cout<<"\n\n\n\nPlayer "<<next<<"'s turn!!!\n\n\n\n\n\n";
本来没有问题的,value一改问题就来了
谁能帮我看看?
file:///C:/Users/User/AppData/Local/Temp/moz-screenshot.jpgfile:///C:/Users/User/AppData/Local/Temp/moz-screenshot-1.jpg |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 04:13 PM
|
显示全部楼层
可以给我整个code吗?
我每onlyonly酱强看半个就可以理解  |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 04:33 PM
|
显示全部楼层
回复 126# Wongkokchoy 的帖子
阿chai。。
你的PROGRAM还没做好“妹”?
对了。。
我队友说过后DR会要我们加她刚教的FILE HANDLING 和FUNCTION。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-10-2008 04:49 PM
|
显示全部楼层
[code]
// 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<<""<<name<<" climb 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 number,char name[] )
{
cin.get();
cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
cout << "\nPlayer [" <<number<< "] - "<<name<<" win!!!!\n\nCongratulation!!!\n\n"<<endl;
cout<<"----------------------------------WINNER---------------------------------------\n";
}
//end of functions definition
{/code] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-10-2008 05:02 PM
|
显示全部楼层
[code]
// 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<<""<<name<<" climb 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 number,char name[] )
{
cin.get();
cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
cout << "\nPlayer [" <<number<< "] - "<<name<<" win!!!!\n\nCongratulation!!!\n\n"<<endl;
cout<<"----------------------------------WINNER---------------------------------------\n";
}
//end of functions definition
{/code] |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 05:20 PM
|
显示全部楼层
WONG的LINE有点问题...
#include // header file
#include
#include
#include
#include
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;
Line(); // function call
cout<<"*******************************************************************************"<<
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<
cout<<" WW WW WW EEEEEE LL CCCCCC OOOOO M M EEEEEE "<
cout<<" WW WWWW WW EE LL CC OO OO M M M M EE "<
cout<<" WW WW WW WW EEEEEE LL CC OO OO M M M M EEEEEE "<
cout<<" WWW WWW EE LL CC OO OO M M EE "<
cout<<" W W EEEEEE LLLLLL CCCCCC OOOO M M EEEEEE "<<<
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<
cout<<"*******************************************************************************"<<
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];
for(int i = 0; i < n ;i++)
name = new char[100];
for (int x=0; x
{
cout<<"Please enter player "<<(x+1)<<"'s name.\n"; //records players' name
cin.getline (name[x], 100);
}
for (int x=0; x
{
cout<<"Player "<<(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;
do
{
for (x=0; x
{
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<<"Press '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\nUser had quit the game."; // exits from the loop when user key in 'q' or 'Q'
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!\n"<<<" 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<<""<<<" had reached "<<<"!\n\n"; // displays usres' position
cin.get();
next = x+1;
cout<<"\n\n\n\n"<<<"'s turn!!!\n\n\n\n\n\n";
}
if (player[x]==100) // exits loops when one of the players reach 100
{
winner (number, name[x]);
break;
}
if (next==n+1)
cout<<"\n\n\n\n"<<<"'s turn!!!\n\n\n\n\n\n";
}
if (toupper(dice)=='Q')
break;
}
while(player[x]!=100); // keep the looping untill a player reach 100
} // end of main funciton
// functions definition |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 06:45 PM
|
显示全部楼层
你是不是要说
before
- next = x+2;
- if (next==n+1)
- next=1;
复制代码
after
- next = x+1;
- if (next==n+1)
- next=0;
复制代码
而新的code run不到??? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-10-2008 07:18 PM
|
显示全部楼层
差不多是酱啰, 之前可以的,换一换value就不行
了。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 08:13 PM
|
显示全部楼层
回复 126# Wongkokchoy 的帖子
logic error
n 是多少个玩家, 比如两个
n = 2,
慢慢来分析
for (x=0; x<n; x++) // save players' position into array "player"next = number;
{
next=x+1;
if (next==n+1)
next=0;
cout<<"\n\n\n\n"<<name[next]<<"'s turn!!!\n\n\n\n\n\n";
}
----------------------
n = 2,
x = 0 的时候,
x<n // true 0 < 2
next = 0 + 1 // next == 1
if (next==n+1) // 1 != 2 + 1
cout<<"\n\n\n\n"<<name[next]<<"'s turn!!!\n\n\n\n\n\n"; // cout << name[1] ?? 应该是 cout << name[0] 吧
x = 1 的时候
x < n // true 1 < 2
next = 1 + 1 // next
if (next==n+1) // 2 != 2 + 1
cout<<"\n\n\n\n"<<name[next]<<"'s turn!!!\n\n\n\n\n\n"; // cout << name[2] ??????? 你只有 name[0] 与 name[1] ,
name[2] 的话当然是memory violation 啊。。。
继续
x = 2,
x < n // false 2 < 2
quit loop,
所以你看, 你的
if (next==n+1)
next=0;
毫无用处, 因为 next 永远不可能 == n + 1 的。
[ 本帖最后由 onlylonly 于 10-10-2008 08:22 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 08:16 PM
|
显示全部楼层
回复 133# Wongkokchoy 的帖子
你要的应该是这个.
将- for (x=0; x<n; x++) // save players' position into array "player"next = number;
- {
- next=x+1;
- if (next==n+1)
- next=0;
- cout<<"\n\n\n\n"<<name[next]<<"'s turn!!!\n\n\n\n\n\n";
- }
复制代码 换成- next = x+1;
- if (next>=n)
- next=0;
- cout << "\n\n\n\n" << name[next] << "'s turn!!!\n\n\n\n\n\n\n";
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 08:17 PM
|
显示全部楼层
回复 135# onlylonly 的帖子
完整的 code- #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;
- Line(); // function call
- cout<<"*******************************************************************************"<<endl<<endl;
- 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];
- for(int i = 0; i < n ;i++)
- name[i] = new char[100];
- for (int x=0; x<n; x++)
- {
- cout<<"Please enter player "<<(x+1)<<"'s name.\n"; //records players' name
- cin.getline (name[x], 100);
- }
- for (int x=0; x<n; x++)
- {
- cout<<"Player "<<(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(); // funciton call
- for (x = 0; x < n; x++) // sets all cell values in the array player to 0
- player[x] = 0;
- do
- {
- for (x=0; x<n; x++) // save players' position into array "player"
- {
- number=x+1;
- cout<<"\n\n\n\n\n\n"<<name[x]<<" is now at position "<<player[x]<<".\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<<"Press '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\nUser had quit the game."; // exits from the loop when user key in 'q' or 'Q'
- 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"<<name[x]<<" gets 1!!\n"; // displays to tell users the number on the 'dice'
- 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 ) // when users' position is over 100, user will reverse to their respective position
- {
- 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<<" 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]) // 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();
- next = x+2;
- }
- 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" << name[next] << "'s turn!!!\n\n\n\n\n\n\n";
- }
- if (toupper(dice)=='Q')
- break;
- }
- 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<<""<<name<<" climb 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 number,char name[] )
- {
- cin.get();
- cout<<"\n\n----------------------------------WINNER---------------------------------------\n";
- cout << "\nPlayer [" <<number<< "] - "<<name<<" win!!!!\n\nCongratulation!!!\n\n"<<endl;
- cout<<"----------------------------------WINNER---------------------------------------\n";
- }
- //end of functions definition
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 08:26 PM
|
显示全部楼层
|
这几天都应该不会常上来了, 连环考试 。。。 没有间隔。。 显了。。 天天都要背书。。 |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 10:30 PM
|
显示全部楼层
小弟新来, 请大家多多指教
看了第一页,觉得这个ASSIGNMENT很有趣,自己试一试。
花了差不多整天
暂时做到n player可以轮流玩,到了5就宣布那一个player win the game.
接下来的迟些再试试。
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define m 50
void Line()
{
cout<<"-------------------------------------------------------------------------------\n";
}
int main()
{
char dice;
int A, n;
int answer[m] = {0};
int player[m];
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";
Line();
cout<<"\n Now please enter the numbers of players:\n";
cin>>n;
cout<<"\nSo there will be "<<<" player(s) in the game.\n";
cout<<"Good Luck and Have Fun!!!\n\n";
cout<<"(All players start at 0)\n\n\n";
//while ((answer1 )<10 && (answer2 <10)&&(answer3 <10))
//{
for (int x=1; x<=n; x++)
{
cout<<"Now is player "<<<"'s turn!"<<"\n";
player[x]=0;
cout<<" Press 't' to throw a dice!\n";
cin>>dice;
if (dice=='t')
srand(time(NULL));
A=rand()%6+1; //Generate dice number
if (A==1)
{
cout<<"\n \n";
cout<<" . \n";
cout<<" \n";
cout<<"\n\nYou get 1!!\n";
}
else if (A==2)
{
cout<<"\n . \n";
cout<<" \n";
cout<<" . \n";
cout<<"\n\nYou get 2!!\n";
}
else if (A==3)
{
cout<<"\n. \n";
cout<<" . \n";
cout<<" .\n";
cout<<"\n\nYou get 3!!\n";
}
else if (A==4)
{
cout<<"\n. .\n";
cout<<" \n";
cout<<". .\n";
cout<<"\n\nYou get 4!!\n";
}
else if (A==5)
{
cout<<"\n. .\n";
cout<<" . \n";
cout<<". .\n";
cout<<"\n\nYou get 5!!\n";
}
else if (A==6)
{
cout<<"\n. .\n";
cout<<". .\n";
cout<<". .\n";
cout<<"\n\nYou get 6!!\n";
}
answer[x]+=A;
cout<<" Player "<<<" had reached "<<<"\n";
if (answer[x]==5)
{
cout<<" Player "<<< " win the game"<
x=n+3 ; //terminate game
}
else if (answer[x]>5)
{
cout<<"more than 5. Freezee one time"<
answer[x]=answer[x]-A;
cout<<" Player "<<<" now at previous location, which is step "<<
if (x==n) // to prevent program terminated unexpecetedly.
x=0;
}
else if (answer[x]<=4)
{
// cout<<"Haven't reach 5. Next player"<
if (x==n) // player 1 take turn again
x=0;
}
}
return 0;
}
[ 本帖最后由 lliioonn 于 10-10-2008 10:35 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 10:43 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 11-10-2008 02:00 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|