主题:[原创]又一个五子棋程序(面向对象的)
这是我们的期末作业,由于才大一,自己又不努力,没有学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();
}
源代码分为四个文件组成,是图形界面的游戏。嘻嘻,贴出来大家交流一下啦,里面一定又很多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();
}