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

楼主 |
发表于 1-10-2008 12:23 PM
|
显示全部楼层
不是很明白,因为我没有学过像下面酱的冬冬(argument):
int main( int argc, char *argv[])
我学的都是void main(),int main()
需要时间消化消化以一下
[ 本帖最后由 Wongkokchoy 于 1-10-2008 12:25 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 12:25 PM
|
显示全部楼层
回复 22# Wongkokchoy 的帖子
那么你就用 int main()
int main( int argc, char *argv[]) 就别管他。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 12:32 PM
|
显示全部楼层
bool
#include <ctime>
using namespace std;
又是什么? |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 12:34 PM
|
显示全部楼层
回复 24# Wongkokchoy 的帖子
|
bool 是 boolean。 也就是 TRUE or FALSE |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 12:36 PM
|
显示全部楼层
回复 24# Wongkokchoy 的帖子
#include <ctime>
与
#include <time.h> 基本上一样 time.h 待会用来更改rand的seed, 不然你的rand 每一次都会给你相同的号码。
using namespace std, 使用namespace, 这里是standard namespace |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 12:39 PM
|
显示全部楼层
很多function没教到,不知道能不能用。。。。。
#include <ctime>
和
using namespace std;
是什么?
然后如果player 1 赢了,可是我要让剩下的player 2 和 player 3 continue玩到完,酱又要如何呢?
[ 本帖最后由 Wongkokchoy 于 1-10-2008 12:42 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 12:44 PM
|
显示全部楼层
回复 27# Wongkokchoy 的帖子
我这里用的 library 只有 srand, rand(), time, 罢了, 其他的都是自己的function。 当然还有cin cout endl
using namespace std; .. 这个是关于namespace的, 现在你可以别管他, 记得每一次写c++ 的时候就加下去。
#include <ctime> 这个加入 time.h 这个library。
在c++ 里 #include <time.h> 不是正规写法 ( 因为不是每一个系统的 filename extention 都是.h) , 说一就写
#include <ctime> |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 12:48 PM
|
显示全部楼层
我尝试放
using namespace std; 进去code 里面, 可是有error:
Namespace name expected |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 01:23 PM
|
显示全部楼层
回复 27# Wongkokchoy 的帖子
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- /************* Function Phototype BEGIN *******************/
- void initialize_player_info( int pos[], bool player_gameover_flag[] , int no_player );
- /* set player starting position
- argument: pos[] `player_position array`
- no_player ` variable that hole " number of player " `
- */
- int player_move( int position );
- /* Player roll dice and move
- If position > 100, reverse
- climb ladder or slide down the snake if any
- argument: position `Player current position`
- return : position `Player position`
- */
- int snake_and_ladder( int position );
- /* Change player position when player meets ladder or snake
- print out statement to inform player
- argument: position `Player current position`
- return : position `Player position after ladder and snake`
- */
- bool gameover( int position );
- /* Check is game over criteria meets?
- argument: position `Player current position`
- return : TRUE ` When game over`
- FALSE ` When not yet game over`
- */
- void print_ladder( int position );
- /* print the statement when player meets a ladder
- argument : position `Player position after meets a ladder`
- return : NONE
- */
- void print_snake( int position );
- /* print the statement when player meets a snake
- argument : position `Player position after meets a snake`
- return : NONE
- */
- void print_gameover( int player );
- /* print out game over statement . i.e who wins
- argument : player `Winning player`
- return : NONE
- */
- bool all_gameover( bool gameover_flag[], int no_player );
- /************** END FUNCTION PHOTOTYPE *****************/
- int main( int argc, char *argv[])
- {
- // randomize rand() seed
- srand( time(0));
- int no_player, *player_position, current_player;
- bool *player_gameover_flag;
- cout << "Enter Number of Players : ";
- cin >> no_player;
- cin.get(); // get rid of enter key "line break"
- player_position = new int[ no_player ];
- player_gameover_flag = new bool [ no_player];
- initialize_player_info( player_position, player_gameover_flag, no_player );
- while( all_gameover( player_gameover_flag, no_player) == false )
- {
- for( current_player = 0; current_player < no_player; current_player++)
- {
- if( player_gameover_flag[ current_player ] == false )
- {
- cout << "\n\nPlayer " << current_player + 1 << " turn : " << endl;
- player_position[ current_player ] = player_move( player_position[ current_player ]);
- }
- else
- continue;
- if ( player_gameover_flag[current_player] = gameover( player_position[ current_player ] ))
- print_gameover( current_player );
- }
- }
-
- return 0;
- }
- // set starting position of all player
- void initialize_player_info( int pos[], bool player_gameover_flag[], int no_player )
- {
- while( --no_player >= 0 )
- {
- player_gameover_flag[ no_player ] = false;
- pos[ no_player ] = 1 ;
- }
- return;
- }
-
- int player_move( int position )
- {
- int dice;
- // roll dice and move
- cout << "Rolling dice .. .. "
- << "Press ENTER to continue ...";
- cin.get();
- dice = rand() % 6 + 1;
- cout << "\n . . \n"
- << ". . \n"
- << ". . \n"
- << "\n\nyou get " << dice << " !!" << endl
- << "\nMoving forward !! " << endl;
-
- position += dice;
- if( position > 100 )
- {
- cout << "OPPS!! \n"
- << "You got to the wall , more than 100 !! \n"
- << "Moving " << position - 100 << " steps backward " << endl;
- position = 100 - ( position - 100 );
- }
- cout << "You are now in position " << position << "!!" << endl;
- position = snake_and_ladder( position );
- return position;
- }
- int snake_and_ladder( int position )
- {
- // put your snake and ladder information here
- // e.g a ladder at position 4, goes to position 16
- // case 4:
- // return 16;
- switch( position )
- {
- case 4:
- print_ladder(16);
- return 16;
- default:
- return position;
- }
- }
- bool gameover( int position )
- {
- if (position == 100)
- return true;
- else
- return false;
- }
- void print_gameover( int player )
- {
- cout << "Player " << player + 1 << " WINS!! " << endl
- << "Congratulation " << endl;
- }
- void print_ladder( int position )
- {
- cout << "Congratulation !! \n"
- << "You climb up a ladder \n"
- << "You are now at position " << position << " !!" << endl;
- }
- void print_snake( int position )
- {
- cout << "OPPS!! \n"
- << "You meets a snake \n"
- << "You are now at position " << position << " !!" << endl;
- }
- bool all_gameover( bool gameover_flag[], int no_player )
- {
- int count, i;
- for( count = 0, i = 0; i < no_player ; i++ )
- if( gameover_flag[i] == true )
- count++;
- if( count < no_player )
- return false;
- else
- return true;
- }
复制代码
[ 本帖最后由 onlylonly 于 1-10-2008 01:41 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 01:23 PM
|
显示全部楼层
回复 29# Wongkokchoy 的帖子
你将 code 贴上来。
----------
改了少许code, 之前忘记 initialize 1 个 variable
[ 本帖最后由 onlylonly 于 1-10-2008 01:39 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 04:10 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 07:05 PM
|
显示全部楼层
回复 32# Wongkokchoy 的帖子
等等,
做莫
#include <iostream>
#include <cstdlib>
#include <ctime>
变成
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
的?
---------------------------------------------------------------
感觉上是 borland c++ 5.02 ?
这个太久了。 老师规定要用这个?
如果或是的话, 那么就将- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
复制代码 改成- #include <iostream.h>
- #include <stdlib.h>
- #include <time.h>
复制代码 ------------
[ 本帖最后由 onlylonly 于 1-10-2008 07:20 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 07:08 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 07:30 PM
|
显示全部楼层
你说对了,老师规定要用borland C++ 5.02,一个老旧的program.....:@
我看了整个下午,还是不懂argv,argc和这个地方:
while( all_gameover( player_gameover_flag, no_player) == false )
{
for( current_player = 0; current_player < no_player; current_player++)
{
if( player_gameover_flag[ current_player ] == false )
{
cout << "\n\nPlayer " << current_player + 1 << " turn : " << endl;
player_position[ current_player ] = player_move( player_position[ current_player ]);
}
else
continue;
if ( player_gameover_flag[current_player] == gameover( player_position[ current_player ] ))
print_gameover( current_player );
}
}
所以就是说我不能用你的code啰?
其实小事像dice,snake 和 ladder 我都可以做到了,只是我不能够让用户们交替丢dice
解决了这个问题我就完成了。
[ 本帖最后由 Wongkokchoy 于 1-10-2008 07:32 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 07:38 PM
|
显示全部楼层
回复 35# Wongkokchoy 的帖子
你可以用, 只是不用 namespace 罢了- #include <iostream.h>
- #include <stdlib.h>
- #include <time.h>
- /************* Function Phototype BEGIN *******************/
- void initialize_player_info( int pos[], bool player_gameover_flag[] , int no_player );
- /* set player starting position
- argument: pos[] `player_position array`
- no_player ` variable that hole " number of player " `
- */
- int player_move( int position );
- /* Player roll dice and move
- If position > 100, reverse
- climb ladder or slide down the snake if any
- argument: position `Player current position`
- return : position `Player position`
- */
- int snake_and_ladder( int position );
- /* Change player position when player meets ladder or snake
- print out statement to inform player
- argument: position `Player current position`
- return : position `Player position after ladder and snake`
- */
- bool gameover( int position );
- /* Check is game over criteria meets?
- argument: position `Player current position`
- return : TRUE ` When game over`
- FALSE ` When not yet game over`
- */
- void print_ladder( int position );
- /* print the statement when player meets a ladder
- argument : position `Player position after meets a ladder`
- return : NONE
- */
- void print_snake( int position );
- /* print the statement when player meets a snake
- argument : position `Player position after meets a snake`
- return : NONE
- */
- void print_gameover( int player );
- /* print out game over statement . i.e who wins
- argument : player `Winning player`
- return : NONE
- */
- bool all_gameover( bool gameover_flag[], int no_player );
- /************** END FUNCTION PHOTOTYPE *****************/
- int main( int argc, char *argv[])
- {
- // randomize rand() seed
- srand( time(0));
- int no_player, *player_position, current_player;
- bool *player_gameover_flag;
- cout << "Enter Number of Players : ";
- cin >> no_player;
- cin.get(); // get rid of enter key "line break"
- player_position = new int[ no_player ];
- player_gameover_flag = new bool [ no_player];
- initialize_player_info( player_position, player_gameover_flag, no_player );
- while( all_gameover( player_gameover_flag, no_player) == false )
- {
- for( current_player = 0; current_player < no_player; current_player++)
- {
- if( player_gameover_flag[ current_player ] == false )
- {
- cout << "\n\nPlayer " << current_player + 1 << " turn : " << endl;
- player_position[ current_player ] = player_move( player_position[ current_player ]);
- }
- else
- continue;
- if ( player_gameover_flag[current_player] = gameover( player_position[ current_player ] ))
- print_gameover( current_player );
- }
- }
-
- return 0;
- }
- // set starting position of all player
- void initialize_player_info( int pos[], bool player_gameover_flag[], int no_player )
- {
- while( --no_player >= 0 )
- {
- player_gameover_flag[ no_player ] = false;
- pos[ no_player ] = 1 ;
- }
- return;
- }
-
- int player_move( int position )
- {
- int dice;
- // roll dice and move
- cout << "Rolling dice .. .. "
- << "Press ENTER to continue ...";
- cin.get();
- dice = rand() % 6 + 1;
- cout << "\n . . \n"
- << ". . \n"
- << ". . \n"
- << "\n\nyou get " << dice << " !!" << endl
- << "\nMoving forward !! " << endl;
-
- position += dice;
- if( position > 100 )
- {
- cout << "OPPS!! \n"
- << "You got to the wall , more than 100 !! \n"
- << "Moving " << position - 100 << " steps backward " << endl;
- position = 100 - ( position - 100 );
- }
- cout << "You are now in position " << position << "!!" << endl;
- position = snake_and_ladder( position );
- return position;
- }
- int snake_and_ladder( int position )
- {
- // put your snake and ladder information here
- // e.g a ladder at position 4, goes to position 16
- // case 4:
- // return 16;
- switch( position )
- {
- case 4:
- print_ladder(16);
- return 16;
- default:
- return position;
- }
- }
- bool gameover( int position )
- {
- if (position == 100)
- return true;
- else
- return false;
- }
- void print_gameover( int player )
- {
- cout << "Player " << player + 1 << " WINS!! " << endl
- << "Congratulation " << endl;
- }
- void print_ladder( int position )
- {
- cout << "Congratulation !! \n"
- << "You climb up a ladder \n"
- << "You are now at position " << position << " !!" << endl;
- }
- void print_snake( int position )
- {
- cout << "OPPS!! \n"
- << "You meets a snake \n"
- << "You are now at position " << position << " !!" << endl;
- }
- bool all_gameover( bool gameover_flag[], int no_player )
- {
- int count, i;
- for( count = 0, i = 0; i < no_player ; i++ )
- if( gameover_flag[i] == true )
- count++;
- if( count < no_player )
- return false;
- else
- return true;
- }
复制代码
[ 本帖最后由 onlylonly 于 1-10-2008 07:57 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 07:43 PM
|
显示全部楼层
原帖由 Wongkokchoy 于 1-10-2008 07:30 PM 发表 
你说对了,老师规定要用borland C++ 5.02,一个老旧的program.....:@
我看了整个下午,还是不懂argv,argc和这个地方:
while( all_gameover( player_gameover_flag, no_player) == false )
{
for( current_player = 0; current_player < no_player; current_player++)
{
if( player_gameover_flag[ current_player ] == false )
{
cout << "\n\nPlayer " << current_player + 1 << " turn : " << endl;
player_position[ current_player ] = player_move( player_position[ current_player ]);
}
else
continue;
if ( player_gameover_flag[current_player] = gameover( player_position[ current_player ] ))
print_gameover( current_player );
}
}
while( all_gameover( player_gameover_flag, no_player) == false )
这行用来检查是否每一个玩家都已经到达终点了
for( current_player = 0; current_player < no_player; current_player++)
这行是用来 switch player 的
if( player_gameover_flag[ current_player ] == false )
{
cout << "\n\nPlayer " << current_player + 1 << " turn : " << endl;
player_position[ current_player ] = player_move( player_position[ current_player ]);
}
else
continue;
如果玩家已经gameover的话, 那么就skip, 不然的话就继续
if ( player_gameover_flag[current_player] = gameover( player_position[ current_player ] ))
print_gameover( current_player );
检查玩家是否到达终点了 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 07:50 PM
|
显示全部楼层
OnlylOnly, 我run到了,可是很奇怪
你试run看看。 |
|
|
|
|
|
|
|
|
|
|
发表于 1-10-2008 07:52 PM
|
显示全部楼层
回复 38# Wongkokchoy 的帖子
什么地方有问题?
我现在在家没有compiler。 下载着。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2008 07:56 PM
|
显示全部楼层
玩家每走一步,就congratulation了。。。。
然后即使玩家已经到100了,玩家还是会继续走。。。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|