主题:[原创]又一个五子棋程序(面向对象的)
yxz149
[专家分:220] 发布于 2005-06-30 11:14:00
这是我们的期末作业,由于才大一,自己又不努力,没有学AI,只有人-人对战了。
源代码分为四个文件组成,是图形界面的游戏。嘻嘻,贴出来大家交流一下啦,里面一定又很多bug,恳请高手指出,谢谢了。
//def.hpp
#include<dos.h>
#include<bios.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define ENTER 0x1c0d
#define SPACE 0x3920
class ONGAME
{
public:
ONGAME(int gdriver=DETECT);
void draw_map(); // Draw the map of Chess
void question();//to ask you to input your names, and judge whether they are legal
~ONGAME();
private:
void init_map(); // init the map according the external file data
void judge(int p_i,int p_j); // judge whthere the winner has come out
void move(int player); // move according to the player's action
void save_history(int s_winner); // save the result to the file "history.dat"
void clear_map(); // clear the map(reset the map).
void controller(); //to control the action of the players
//name1[] is player1, name2[] is player2
char name1[255],name2[255];
//map_i[] and map_j[] store the posistion on the map;
int map_i[8],map_j[8];
// status[][] record the status 0(none), 1 (player1),2 (player2),
int status[8][8];
//WINNER =1 means player1 wins,WINNER=2 means player2 wins
//WINNER =3 means to escape
//WINNER =(others) means except the 3 cases above
int WINNER;
// first_go has two values 1 or 2, get from the last line of file "map.dat"
int first_go;
};
class GRAPHIC
{
public:
GRAPHIC(int gdriver=DETECT);
void draw_interface(); // Draw the interface(menus)
int moveup(); // when returns 1 means menu1,2 means menu2,....
int movedown(); // when returns 1 means menu1,2 means menu2,....
int enter(); // when returns 1 means entering menu1,2 means menu2,....
~GRAPHIC(); //Close the graphic system
int menu; // the current menu item
int start_x,start_y;
char *show_choice;
ONGAME og; // an object of the class ONGAME
private:
void view_history(); //view the history from the file "history.dat"
void show_about(); // show the content of menu of "about the game"
};
//GRAPHIC.CPP
#include<dos.h>
#include<bios.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<io.h>
#include "def.hpp"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define ENTER 0x1c0d
#define SPACE 0x3920
GRAPHIC::GRAPHIC (int gdriver)
{
int gmode;
initgraph(&gdriver,&gmode,"");
}
void GRAPHIC::draw_interface() // Draw the interface,(menus)
{
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy()); // clear the previous pictures
setcolor(7);
start_x=int(getmaxx()/2)-150;
start_y=int(getmaxy()/2)-150;
rectangle(start_x,start_y,start_x+300,start_y+250);
setfillstyle(6,9);
bar3d(start_x+5,start_y+5,start_x+295,start_y+245,1,1);
setfillstyle(9,10);
bar(start_x+10,start_y+25,start_x+290,start_y+240);
outtextxy(start_x+35,start_y+10,"FIVE-STARS CHESS version 1.0");
setfillstyle(1,BLACK);
setcolor(getmaxcolor());
bar(start_x+30,start_y+40,start_x+270,start_y+65);// menu 1
outtextxy(start_x+50,start_y+50," Start The Game");
setfillstyle(1,6);
bar(start_x+30,start_y+85,start_x+270,start_y+110);// menu 2
outtextxy(start_x+50,start_y+95," Review The History");
bar(start_x+30,start_y+85+45,start_x+270,start_y+110+45);// menu 3
outtextxy(start_x+50,start_y+95+45," About The Game");
bar(start_x+30,start_y+85+45+45,start_x+270,start_y+110+45+45);// menu 4
outtextxy(start_x+50,start_y+95+45+45," Exit the Game");
outtextxy(start_x+45,start_y+95+45+45+30,"You are choosing: Start...");
}
int GRAPHIC:: moveup()// when returns 1 means menu1,2 means menu2,....
{
int current_menu;
switch (menu)
{
case 1:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+85+45+45,start_x+270,start_y+110+45+45);// menu 4
outtextxy(start_x+50,start_y+95+45+45," Exit the Game");
setfillstyle(1,6);
bar(start_x+30,start_y+40,start_x+270,start_y+65);// menu 1
outtextxy(start_x+50,start_y+50," Start The Game");
show_choice="Exit...";
current_menu=4;
break;
case 2:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+40,start_x+270,start_y+65);// menu 1
outtextxy(start_x+50,start_y+50," Start The Game");
setfillstyle(1,6);
bar(start_x+30,start_y+85,start_x+270,start_y+110);// menu 2
outtextxy(start_x+50,start_y+95," Review The History");
show_choice="Start...";
current_menu=1;
break;
case 3:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+85,start_x+270,start_y+110);// menu 2
outtextxy(start_x+50,start_y+95," Review The History");
setfillstyle(1,6);
bar(start_x+30,start_y+85+45,start_x+270,start_y+110+45);// menu 3
outtextxy(start_x+50,start_y+95+45," About The Game");
show_choice="Review...";
current_menu=2;
break;
case 4:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+85+45,start_x+270,start_y+110+45);// menu 3
outtextxy(start_x+50,start_y+95+45," About The Game");
setfillstyle(1,6);
bar(start_x+30,start_y+85+45+45,start_x+270,start_y+110+45+45);// menu 4
outtextxy(start_x+50,start_y+95+45+45," Exit the Game");
show_choice="About...";
current_menu=3;
break;
}
setfillstyle(9,10);
bar(start_x+185,start_y+95+45+45+30,start_x+260,start_y+95+45+45+40);
outtextxy(start_x+185,start_y+95+45+45+30,show_choice);
return current_menu;
}
int GRAPHIC::movedown() // when returns 1 means menu1,2 means menu2,....
{
int current_menu;
switch(menu)
{
case 1:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+85,start_x+270,start_y+110);// menu 2
outtextxy(start_x+50,start_y+95," Review The History");
setfillstyle(1,6);
bar(start_x+30,start_y+40,start_x+270,start_y+65);// menu 1
outtextxy(start_x+50,start_y+50," Start The Game");
show_choice="Review...";
current_menu=2;
break;
case 2:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+85+45,start_x+270,start_y+110+45);// menu 3
outtextxy(start_x+50,start_y+95+45," About The Game");
setfillstyle(1,6);
bar(start_x+30,start_y+85,start_x+270,start_y+110);// menu 2
outtextxy(start_x+50,start_y+95," Review The History");
show_choice="About...";
current_menu=3;
break;
case 3:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+85+45+45,start_x+270,start_y+110+45+45);// menu 4
outtextxy(start_x+50,start_y+95+45+45," Exit the Game");
setfillstyle(1,6);
bar(start_x+30,start_y+85+45,start_x+270,start_y+110+45);// menu 3
outtextxy(start_x+50,start_y+95+45," About The Game");
show_choice="Exit...";
current_menu=4;
break;
case 4:
setfillstyle(1,BLACK);
bar(start_x+30,start_y+40,start_x+270,start_y+65);// menu 1
outtextxy(start_x+50,start_y+50," Start The Game");
setfillstyle(1,6);
bar(start_x+30,start_y+85+45+45,start_x+270,start_y+110+45+45);// menu 4
outtextxy(start_x+50,start_y+95+45+45," Exit the Game");
show_choice="Start...";
current_menu=1;
break;
}
setfillstyle(9,10);
bar(start_x+185,start_y+95+45+45+30,start_x+260,start_y+95+45+45+40);
outtextxy(start_x+185,start_y+95+45+45+30,show_choice);
return current_menu;
}
int GRAPHIC::enter() // when returns 1 means entering menu1,2 means menu2,....
{
int current_menu;
if (menu==1) //call "start the game"
og.question();
if (menu==2) //call "Review the history"
{
view_history();
}
if (menu==3) //call "About the game"
{
show_about();
}
if (menu==4) //call "exit the game"
{
exit(1);
}
return current_menu;
}
GRAPHIC:: ~GRAPHIC() //Close the graphic system
{
closegraph();
}
void GRAPHIC::view_history() // view the history from the file "history.dat"
{
ifstream read_from_history_file;
char *name;
int item_num=0;
name=new char[255];
char player1[255],player2[255];
int year,month,day;
read_from_history_file.open("history.dat",ios::in|ios::binary);
if (!read_from_history_file)
{
cout<<"file \"history.dat\" not exist!";
getch();
return;
}
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy());
cout.unsetf(ios::hex);
cout<<endl<<endl;
while (!read_from_history_file.eof())
{
item_num++;
read_from_history_file>>player1;
read_from_history_file>>player2;
read_from_history_file>>name;
if (player1[0]!='\0')
{
cout<<"----------------------------------------------------"<<endl;
cout<<player1<<" VS "<<player2<<", and the Winner is : "<<name<<endl<<endl;
}
if (item_num%8==0)
{
setfillstyle(2,3);
bar(260-30,20,440-30,30);
outtextxy(280-30,20,"Review The History");
cout<<"Press any to continue..."<<endl;
getch();
}
}
cout<<"----------------------------------------------------"<<endl;
cout<<endl<<"Press any key to exit.";
setfillstyle(2,3);
bar(260-30,20,440-30,30);
outtextxy(280-30,20,"Review The History");
getch();
read_from_history_file.close();
menu=1;
draw_interface();
}
void GRAPHIC::show_about()
{
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy());
setfillstyle(1,WHITE);
bar(getmaxx()/2-150,getmaxy()/2-70,getmaxx()/2+150,getmaxy()/2+70);
setcolor(BLACK);
outtextxy(getmaxx()/2-150+100,getmaxy()/2-70+5,"About the Game");
setfillstyle(1,0);
bar(getmaxx()/2-150+5,getmaxy()/2-70+20,getmaxx()/2+150-5,getmaxy()/2+65);
setcolor(WHITE);
outtextxy(getmaxx()/2-150+50,getmaxy()/2-70+40-5," Five-Star Chess 1.0");
outtextxy(getmaxx()/2-150+50,getmaxy()/2-70+60-5," Coded by Alex Ye 2005.6 ");
outtextxy(getmaxx()/2-150+40,getmaxy()/2-70+80-5,"CAS of Sun Yat Sen University");
outtextxy(getmaxx()/2-150+20,getmaxy()/2-70+100-5,"QQ:125165304 Email:yxz149@163.com");
outtextxy(getmaxx()/2-150+40,getmaxy()/2-70+120-5," http://tqsoft.178L.com");
getch();
menu=1;
draw_interface();
}
沙发
yxz149 [专家分:220] 发布于 2005-06-30 11:16:00
//ONGAME.CPP
#include<dos.h>
#include<bios.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<string.h>
#include "def.hpp"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define ENTER 0x1c0d
#define SPACE 0x3920
#define F1 0x3b00
ONGAME::ONGAME(int gdriver)
{
int gmode;
int i;
initgraph(&gdriver,&gmode,"");
for (i=0;i<8;i++) // create the map_x[] and map_y[] position(the graphic position)
{
map_i[i]=90+40*i;
map_j[i]=60+40*i;
}
}
ONGAME::~ONGAME()
{
closegraph();
}
void ONGAME::question() //to ask you to input your names, and judge whether they are legal
{
int key;
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy()); // clear the previous pictures
moveto(100,130);
setfillstyle(1,WHITE);
bar(getmaxx()/2-100,getmaxy()/2-65,getmaxx()/2+100,getmaxy()/2-50);
bar(getmaxx()/2-100,getmaxy()/2-65+100,getmaxx()/2+100,getmaxy()/2-50+100);
setcolor(0);
outtextxy(getmaxx()/2-100,getmaxy()/2-53+90," EXIT=ESC START=SPACE");
setcolor(WHITE);
rectangle(getmaxx()/2-100,getmaxy()/2-50,getmaxx()/2+100,getmaxy()/2+50);
outtext(" HELLO! BE RELAXED AND ENGJOY YOURSELVES");
setcolor(BLACK);
moveto(getmaxx()/2-60,getmaxy()/2-60);
outtext("INPUT YOUR NAMES");
setcolor(YELLOW);
moveto(getmaxx()/2-75,getmaxy()/2-40+10);
outtext("Player1: ");
moveto(getmaxx()/2-75,getmaxy()/2-40+30+15);
outtext("Player2: ");
do
{
setfillstyle(1,0);
bar(getmaxx()/2-100,getmaxy()-160,getmaxx()/2-60+200,getmaxy()-160+30);
window(41,14,45,14);
cin>>name1;
window(41,16,45,16);
cin>>name2;
setcolor(WHITE);
if (strcmp(name1,name2)==0)
{
outtextxy(getmaxx()/2-60-30,getmaxy()-160,"Names must be different!");
getch();
}
}while (strcmp(name1,name2)==0);
key=0x0001;
while (!((key==SPACE)||(key==ESC)))
{
key=bioskey(0);
cout.setf(ios::hex);
if (key==SPACE) //start the game
draw_map();
else if (key==ESC) return;
}
}
void ONGAME::draw_map() // Draw the map of Chess
{
int i;
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy()); // clear the previous pictures
setcolor(getmaxcolor());
line(0,20,getmaxx(),20); //top line
line(0,getmaxy()-40,getmaxx(),getmaxy()-40); //bottom line
outtextxy(230,10,"FIVE-STARS CHESS 1.0");
line(getmaxx()-200,40,getmaxx()-200,getmaxy()-50);
outtextxy(getmaxx()-200+20,60,"PLAYER1: ");
setfillstyle(1,BROWN);
fillellipse(getmaxx()-200+40,100,10,10);
outtextxy(getmaxx()-200+60,100,name1);
outtextxy(getmaxx()-200+20,140,"PLAYER2:");
outtextxy(getmaxx()-200+20,getmaxy()-140,"NEW=SPACE | F1=Reset");
outtextxy(getmaxx()-200+20,getmaxy()-100,"EXIT=ESC | Alex Ye");
setfillstyle(1,WHITE);
fillellipse(getmaxx()-200+40,180,10,10);
outtextxy(getmaxx()-200+60,180,name2);
setfillstyle(1,9);
bar3d(5,45,getmaxx()-245,getmaxy()-60,3,2);
setcolor(YELLOW);
for (i=70;i<=getmaxy()-50;i=i+40) //draw lines
line(10+30,i,getmaxx()-280,i);
for (i=30;i<=getmaxx()-250-20;i=i+40) //draw lines
line(i+10,70,i+10,getmaxy()-90);
WINNER=4; //here WINNER can not be 1,2 or 3, others are ok
init_map(); // call init_map() to initialize the map accroding to external file "map.dat"
}
void ONGAME::init_map() // init the map according the external file data
{
ifstream read_from_file;
int i,j;
int key;
read_from_file.open("map.dat",ios::binary);
if (!read_from_file)
return;
for (i=0;i<8;i++)
for (j=0;j<8;j++)
read_from_file>>status[i][j];
read_from_file>>first_go;
read_from_file.close();
setfillstyle(1,BROWN);
for (i=0;i<8;i++)
for (j=0;j<8;j++)
{
if (status[i][j]==1)
{
setfillstyle(1,BROWN);
fillellipse(map_j[j],map_i[i],10,10);
}
else if (status[i][j]==2)
{
setfillstyle(1,WHITE);
fillellipse(map_j[j],map_i[i],10,10);
}
}
controller(); //call controller()
}
///////////////////////////////////////
void ONGAME::controller() // to control the action of the players
{
ofstream write_to_file;
int i,j;
int is_save; //is_save='y' to save,is_save='n' not to save, other useless
while ((WINNER!=1)&&(WINNER!=2))
{
move(first_go); //fisrt player to move
if (WINNER==3) //WINNER==3 means escape(Pressing the "ESC")
{
do
{
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy());
setcolor(WHITE);
outtextxy(getmaxx()/2-100,getmaxy()/2,"Do you want to save?(y/n)");
is_save=bioskey(0);
if (is_save==0x1579) //0x1579 means 'y'
{
write_to_file.open("map.dat",ios::trunc);
if (!write_to_file)
{
cout<<"File not exist!";
getch();
break;
}
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
write_to_file<<status[i][j]<<" ";
}
write_to_file<<"\n";
}
write_to_file<<first_go;
write_to_file.close();
break;
}
else if (is_save==0x316e) //0x316e means 'n'
{
break;
}
}while ((is_save!=0x316e)&&(is_save!=0x1579));
break;
}
else if (WINNER!=0) // indicates that there is a winner,(winner a is player1)
{
getch();
break;
}
move(3-first_go); // 3-first_go means the second one to go;
if (WINNER==3) //WINNER==3 means escape
{
do
{
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy());
setcolor(WHITE);
outtextxy(getmaxx()/2-100,getmaxy()/2,"Do you want to save?(y/n)");
is_save=bioskey(0);
if (is_save==0x1579) //0x1579 means 'y'
{
write_to_file.open("map.dat",ios::trunc);
if (!write_to_file)
{
cout<<"File not exist!";
getch();
break;
}
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
write_to_file<<status[i][j]<<" ";
}
write_to_file<<"\n";
}
write_to_file<<3-first_go;
write_to_file.close();
break;
}
else if (is_save==0x316e) //0x316e means 'n'
{
break;
}
}while ((is_save!=0x316e)&&(is_save!=0x1579));
break;
}
}
}
板凳
yxz149 [专家分:220] 发布于 2005-06-30 11:16:00
void ONGAME::move(int player) // move according to the player's action
{
int key;
int color;
//next to judge whether a step is finished, next is 0 means unfishihed
int next=0;
static int p_i=0,p_j=0;
setcolor(9);
circle(map_j[p_j],map_i[p_i],13);
switch(player)
{
case 1: color=BROWN; break;
case 2: color=WHITE; break;
}
setcolor(color);
circle(map_j[p_j],map_i[p_i],13);
do
{
key=bioskey(0);
next=0;
switch(key)
{
case UP:
if (p_i-1>=0)
{
setcolor(9);
circle(map_j[p_j],map_i[p_i],13);
p_i=p_i-1;
setcolor(color);
circle(map_j[p_j],map_i[p_i],13);
}
break;
case DOWN:
if (p_i+1<=7)
{
setcolor(9);
circle(map_j[p_j],map_i[p_i],13);
p_i=p_i+1;
setcolor(color);
circle(map_j[p_j],map_i[p_i],13);
}
break;
case LEFT:
if (p_j-1>=0)
{
setcolor(9);
circle(map_j[p_j],map_i[p_i],13);
p_j=p_j-1;
setcolor(color);
circle(map_j[p_j],map_i[p_i],13);
}
break;
case RIGHT:
if (p_j+1<=7)
{
setcolor(9);
circle(map_j[p_j],map_i[p_i],13);
p_j=p_j+1;
setcolor(color);
circle(map_j[p_j],map_i[p_i],13);
}
break;
case SPACE:
if (status[p_i][p_j]==0)
{
setfillstyle(1,color);
setcolor(WHITE);
fillellipse(map_j[p_j],map_i[p_i],10,10);
status[p_i][p_j]=player;
next=1;
}
judge(p_i,p_j); //jugde whether the winner has come out
break;
case F1:
clear_map();
draw_map();
next=1;
break;
case ESC:
WINNER=3;
return;
default: break;
}
}while (next==0);
}
void ONGAME::judge(int p_i,int p_j)
{
//winner is 0 means no result has come out yet,
//winner is 1 means player1 has won
//winner is 2 means player2 has won
int winner=0;
WINNER=0;
int counter;
int i,j,i_next,j_next;
int start_i,start_j,end_i,end_j;
/*******************************************************************
4 cases to jugde whether the winner has come out,
these cases are divided by the 4 dicrections centering the current
position, and the directions are of wise-clock.
********************************************************************/
// case 1: j is fixed
j=p_j;
counter=1;
for (i=0;i<7;i++)
for (i_next=i+1;i_next<8;i_next++)
{
if (status[i][j]==status[p_i][p_j])
{
if (status[i][j]==status[i_next][j]) counter++;
else if(counter>=5) {i_next=8; i=7;}
else if ((status[i][j]!=status[i_next][j])&&(counter<5))
{
counter=0;
i_next=8; //exit the loop for i_next
}
}
}
if (counter>=5)
winner=status[p_i][p_j];
////////////////////////////////////////
//case 2: i and j is changable
if (p_i+p_j<=7)
{
start_i=0;
end_i=p_i+p_j;
}
else
{
start_i=p_i+p_j-7;
end_i=7;
}
counter=1;
for (i=start_i;i<=end_i-1;i++)
{
counter=1;
for (i_next=i+1;i_next<=end_i;i_next++)
{
j=p_i+p_j-i;
j_next=p_i+p_j-i_next;
if (status[i][j]==status[p_i][p_j])
{
if (status[i][j]==status[i_next][j_next]) counter++;
if (counter>=5) {i_next=end_i+1; i=end_i; }
else if ((counter<5)&&(status[i][j]!=status[i_next][j_next]))
{
counter=0;
i_next=end_i+1;
}
}
}
}
if (counter>=5)
winner=status[p_i][p_j];
///////////////////////////////
// case 3: i is fixed
counter=1;
i=p_i;
for (j=0;j<7;j++)
for (j_next=j+1;j_next<8;j_next++)
{
if (status[i][j]==status[p_i][p_j])
{
if (status[i][j]==status[i][j_next]) counter++;
else if ((status[i][j]!=status[i][j_next])&&(counter<5))
{
counter=0;
j_next=8;
}
else if (counter>=5)
{
j_next=8;
j=7;
}
}
}
if (counter>=5)
winner=status[p_i][p_j];
////////////////////////////////////
//case 4: i and j is changable
if (p_i>=p_j)
{
start_j=0;
end_j=7-(p_i-p_j);
}
else
{
start_j=p_j-p_i;
end_j=7;
}
counter=1;
for (j=start_j;j<=end_j-1;j++)
{
counter=1;
for (j_next=j+1;j_next<=end_j;j_next++)
{
i=p_i-p_j+j;
i_next=p_i-p_j+j_next;
if (status[i][j]==status[p_i][p_j])
{
if (status[i][j]==status[i_next][j_next]) counter++;
if ((status[i][j]!=status[i_next][j_next])&&(counter<5))
{
counter=0;
j_next=end_j+1;
}
else if (counter>=5)
{
j_next=end_j+1;
j=end_j;
}
}
}
}
if (counter>=5) winner=status[p_i][p_j];
if (winner!=0)
{
setcolor(RED);
outtextxy(getmaxx()-200+30,getmaxy()-230,"THe Winner Is:");
outtextxy(getmaxx()-200+20,getmaxy()-200," ");
outtextxy(getmaxx()-200+15,getmaxy()-200+35,"Press any key to exit");
if (winner==1)
{
outtextxy(getmaxx()-200+20,getmaxy()-200," Player1 ");
WINNER=1;
save_history(winner);
}
if (winner==2)
{
outtextxy(getmaxx()-200+20,getmaxy()-200," Player2 ");
WINNER=2;
save_history(winner);
}
}
}
void ONGAME::save_history(int s_winner) // save the result to the file "history.dat"
{
ofstream write_to_history_file;
char *winner_name;
winner_name=new char[255];
write_to_history_file.open("history.dat",ios::app);
if (!write_to_history_file)
{
cout<<"\"history.dat\" file not exist!";
getch();
return;
}
if (s_winner==1)
strcpy(winner_name,name1);
else strcpy(winner_name,name2);
write_to_history_file<<name1<<" "<<name2<<" "<<winner_name<<"\n";
write_to_history_file.close();
}
void ONGAME::clear_map() // clear up (reset) the map.
{
int i;
int j;
ofstream write_to_file;
write_to_file.open("map.dat",ios::trunc);
for (i=0;i<8;i++)
for (j=0;j<8;j++)
status[i][j]=0;
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
write_to_file<<0<<" ";
}
write_to_file<<'\n';
}
write_to_file<<1<<'\n';
write_to_file.close();
}
//main.cpp
#include<dos.h>
#include<bios.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include "def.hpp"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define ENTER 0x1c0d
#define SPACE 0x3920
void main()
{
int key;
int menu;
GRAPHIC g;
g.draw_interface();
g.menu=1;
// the circulation can not stop until you choose "exit" menu
while (1)
{
key=bioskey(0);
if ((key==UP)||(key==DOWN)||(key==ENTER))
{
if (key==UP)
{
menu=g.moveup();
g.menu=menu;
}
else if (key==DOWN)
{
menu=g.movedown();
g.menu=menu;
}
else if (key==ENTER)
{
g.enter();
g.draw_interface();
}
}
}
}
//注,此外程序的运行还需另外两个文件:
history.dat记录战果的
map.dat
其结构如下:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1
当然内容将可能跟其玩家玩之后设置有关。
以上就是我们这学期刚好学完c++后老师给的作业。