mfc五子棋游戏源码(c++编程游戏五子棋程序)
本文目录一览:
急:求一用MFC程序做的小游戏代码
c#写的五子棋程序,供学习WinForms的鼠标事件和使用GDI+
2001-5-4 12:54:00 中文C#技术站
前几天没事,写了一个小程序,可以用于学习C#。
程序使用了VS.NET环境编译,你的机器只要安装了.NET Framework SDK就可以运行。
源码和执行文件可以下载
你不想下载也可读一下源码(图片资源等需要下载)。
namespace Leimom.FiveChess
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;
///
/// Summary description for Form1.
///
public class FiveForm : System.WinForms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components;
private System.WinForms.ImageList imageListbw;
//define the hot Rectangle
private Rectangle[] pointSquares;
//chess information
private int[] chessTable;
private int nextTurn;
private const int bTurn = 1;
private const int wTurn = 2;
private Stack chessIndex;
public FiveForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
chessIndex = new Stack();
nextTurn = bTurn;
chessTable = new int[225];
pointSquares = new Rectangle[225];
Size size = new Size(18,18);
int x = 0;
int y = 0;
for(int i = 0;i 225;i++)
{
x = i%15;
y = i/15;
pointSquares[i].Size = size;
pointSquares[i].Offset(9+x*20,6+y*20);
chessTable[i] = 0;
}
}
protected override void OnPaint(PaintEventArgs e)
{
//you may paint
Graphics g = e.Graphics;
}
protected override void OnMouseDown(System.WinForms.MouseEventArgs e)
{
switch( e.Button )
{
//take left button down
case MouseButtons.Left:
OnLButtonDown(new Point(e.X,e.Y));
break;
//take right button down
case MouseButtons.Right:
OnRButtonDown(new Point(e.X,e.Y));
break;
}
base.OnMouseDown(e);
}
private void OnLButtonDown(Point p)
{
int nPos = GetRectID(p);
//click hot Rectangle witch have no chess
if(nPos != -1chessTable[nPos] == 0)
{
Graphics g = this.CreateGraphics();
if(nextTurn==bTurn)
{
//draw white chess
DrawBlack(g,nPos);
chessTable[nPos] = bTurn;
nextTurn = wTurn;
chessIndex.Push(bTurn);
chessIndex.Push(nPos);
}
else
{
//draw Black chess
DrawWhite(g,nPos);
chessTable[nPos] = wTurn;
nextTurn = bTurn;
chessIndex.Push(wTurn);
chessIndex.Push(nPos);
}
g.Dispose();
//witch win
CheckGameResult(nPos,nextTurn);
}
}
private void CheckGameResult(int nPos,int nextTurn)
{
//witch win
Stack isFive = new Stack();
int thisTurn = (nextTurn == bTurn)?wTurn:bTurn;
int x = nPos%15;
int y = nPos/15;
//scan x have five
for(int i=0;i15;i++)
{
if(chessTable[y*15+i] == thisTurn)
{
isFive.Push(y*15+i);
if(isFive.Count == 5)
{
MessageBox.Show("Game Over","Notes",MessageBox.OK);
ReSetGame();
return;
}
}
else
{
isFive.Clear();
}
}
isFive.Clear();
//scan y have five
for(int i=0;i15;i++)
{
if(chessTable[i*15+x] == thisTurn)
{
isFive.Push(i*15+x);
if(isFive.Count == 5)
{
MessageBox.Show("Game Over","Notes",MessageBox.OK);
ReSetGame();
return;
}
}
else
{
isFive.Clear();
}
}
isFive.Clear();
//scan x=y have five
for(int i=-14;i15;i++)
{
if(x+i0||x+i14||y-i0||y-i14)
{
continue;
}
else
{
if(chessTable[(y-i)*15+x+i] == thisTurn)
{
isFive.Push((y-i)*15+x+i);
if(isFive.Count == 5)
{
MessageBox.Show("Game Over","Notes",MessageBox.OK);
ReSetGame();
return;
}
}
else
{
isFive.Clear();
}
}
}
isFive.Clear();
//scan x=-y have five
for(int i=-14;i15;i++)
{
if(x+i0||x+i14||y+i0||y+i14)
{
continue;
}
else
{
if(chessTable[(y+i)*15+x+i] == thisTurn)
{
isFive.Push((y+i)*15+x+i);
if(isFive.Count == 5)
{
MessageBox.Show("Game Over","Notes",MessageBox.OK);
ReSetGame();
return;
}
}
else
{
isFive.Clear();
}
}
}
isFive.Clear();
}
private void ReSetGame()
{
//reset game
nextTurn = bTurn;
for(int i=0;i225;i++)
{
chessTable[i] = 0;
}
this.Invalidate();
}
private int GetRectID(Point p)
{
//get witch rectangle click
for(int i = 0;i 225;i++)
{
if(pointSquares[i].Contains( p ))
{
return i;
}
}
return -1;
}
private void OnRButtonDown(Point p)
{
//regret chess
int nPos,x,y;
if(chessIndex.Count != 0)
{
nPos = (int)chessIndex.Pop();
x = nPos%15;
y = nPos/15;
chessTable[nPos] = 0;
nextTurn = (int)chessIndex.Pop();
this.Invalidate(new Rectangle(new Point(8+x*20,5+y*20),new Size(20,20)));
}
}
private void DrawBlack(Graphics g,int nPos)
{
//draw Black chess
int x,y;
x = nPos%15;
y = nPos/15;
imageListbw.DrawImage(g,8+20*x,5+20*y,20,20,0,0);
}
private void DrawWhite(Graphics g,int nPos)
{
//draw White chess
int x,y;
x = nPos%15;
y = nPos/15;
imageListbw.DrawImage(g,8+20*x,5+20*y,20,20,0,1);
}
///
/// Clean up any resources being used.
///
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof(FiveForm));
this.components = new System.ComponentModel.Container ();
this.imageListbw = new System.WinForms.ImageList ();
//@this.TrayHeight = 90;
//@this.TrayLargeIcon = false;
//@this.TrayAutoArrange = true;
//@imageListbw.SetLocation (new System.Drawing.Point (7, 7));
imageListbw.ImageSize = new System.Drawing.Size (20, 20);
imageListbw.ImageStream = (System.WinForms.ImageListStreamer) resources.GetObject ("imageListbw.ImageStream");
imageListbw.ColorDepth = System.WinForms.ColorDepth.Depth8Bit;
imageListbw.TransparentColor = System.Drawing.Color.Yellow;
this.Text = "FiveForm";
this.MaximizeBox = false;
this.AutoScaleBaseSize = new System.Drawing.Size (6, 14);
this.BorderStyle = System.WinForms.FormBorderStyle.FixedSingle;
this.BackgroundImage = (System.Drawing.Image) resources.GetObject ("$this.BackgroundImage");
this.TransparencyKey = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size (314, 311);
}
///
/// The main entry point for the application.
///
public static int Main(string[] args)
{
Application.Run(new FiveForm());
return 0;
}
}
}
找五子棋源代码c++
#include "iostream"
#include iomanip
using namespace std;
const int M=20;
const int N=20;
int main()
{
char weizhi[M][N];
int k,i,j,x,y,flag=0;
cout"欢迎使用简易双人对战五子棋游戏"endl;
cout"五子棋棋谱如下:"endl;
for(k=0;k=N;k++)
coutsetw(3)setfill(' ')k;
coutendl;
for(i=1;i=M;i++)
{
coutsetw(3)setfill(' ')i;
for(j=1;j=N;j++)
{
weizhi[i][j]='-';
coutsetw(3)setfill(' ')weizhi[i][j];
}
coutendl;
}
while(flag==0)
{
//红方落子
cout"请红方输入落子位置:"endl;
loop1:
cout"请输入落子的行数:";
cinx;
cout"请输入落子的列数:";
ciny;
if(weizhi[x][y]=='-')
{
weizhi[x][y]='*';
for(k=0;k=N;k++)
coutsetw(3)setfill(' ')k;
coutendl;
for(i=1;i=M;i++)
{
coutsetw(3)setfill(' ')i;
for(j=1;j=N;j++)
coutsetw(3)setfill(' ')weizhi[i][j];
coutendl;
}
}
else
{
cout"你不能在这落子,请重新选择落子位置:"endl;
goto loop1;
}
//判断胜利
for(i=1;i=M-4;i++)
{
for(j=1;j=N-4;j++)
{
if(weizhi[i][j]=='*' weizhi[i][j+1]=='*' weizhi[i][j+2]=='*' weizhi[i][j+3]=='*' weizhi[i][j+4]=='*')
{
cout"恭喜红方获得简易双人对战五子棋的胜利!耶~~~"endl;
flag=1;
break;
}
if(weizhi[i][j]=='*' weizhi[i+1][j]=='*' weizhi[i+2][j]=='*' weizhi[i+3][j]=='*' weizhi[i+4][j]=='*')
{
cout"恭喜红方获得简易双人对战五子棋的胜利!耶~~~"endl;
flag=1;
break;
}
if(weizhi[i][j]=='*' weizhi[i+1][j+1]=='*' weizhi[i+2][j+2]=='*' weizhi[i+3][j+3]=='*' weizhi[i+4][j+4]=='*')
{
cout"恭喜红方获得简易双人对战五子棋的胜利!耶~~~"endl;
flag=1;
break;
}
if(flag==1)
break;
}
}
//蓝方落子
cout"请蓝方输入落子位置:"endl;
loop2:
cout"请输入落子的行数:";
cinx;
cout"请输入落子的列数:";
ciny;
if(weizhi[x][y]=='-')
{
weizhi[x][y]='#';
for(k=0;k=N;k++)
coutsetw(3)setfill(' ')k;
coutendl;
for(i=1;i=M;i++)
{
coutsetw(3)setfill(' ')i;
for(j=1;j=N;j++)
coutsetw(3)setfill(' ')weizhi[i][j];
coutendl;
}
}
else
{
cout"你不能在这落子,请重新选择落子位置:";
goto loop2;
}
//判断胜利
for(i=1;i=M-4;i++)
{
for(j=1;j=N-4;j++)
{
if(weizhi[i][j]=='#' weizhi[i][j+1]=='#' weizhi[i][j+2]=='#' weizhi[i][j+3]=='#' weizhi[i][j+4]=='#')
{
cout"恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~"endl;
flag=1;
break;
}
if(weizhi[i][j]=='#' weizhi[i+1][j]=='#' weizhi[i+2][j]=='#' weizhi[i+3][j]=='#' weizhi[i+4][j]=='#')
{
cout"恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~"endl;
flag=1;
break;
}
if(weizhi[i][j]=='#' weizhi[i+1][j+1]=='#' weizhi[i+2][j+2]=='#' weizhi[i+3][j+3]=='#' weizhi[i+4][j+4]=='#')
{
cout"恭喜蓝方获得简易双人对战五子棋的胜利!耶~~~"endl;
flag=1;
break;
}
if(flag==1)
break;
}
}
}
return 0;
}
我运行过,没有错误.
求vc6.0界面下的五子棋源代码
这个太多了啊,你要vc下的,那基本都会用mfc的,因为vc没有画图函数啊,又TC3.0的你要不要
* 运行环境:TC3.0 (vc6.0不支持画图函数,无法画出棋盘和棋子)
************************************************************* #include
#include
#include
#include
#define backcolor CYAN
#define defaultcolor BLACK
#define linecolor MAGENTA
#define player1_color RED
#define player2_color WHITE
#define error_color RED
#define winner_color RED
const int left=40;
const int top=390;
const int d=30;
const int line_num=9;
const int turn=0;
const int r=d/3;
const int j=10;
int x,y,k=1,step=(line_num+1)*(line_num+1);
union REGS regs1,regs2;
class player1;
class player2;
class qipan{
public:
qipan();
~qipan(){};
void init_qipan();
friend void fall(player1 num1,player2 num2,qipan num);
friend void input(player1 num1,player2 num2,qipan num);
private:
int point[line_num+1][line_num+1];
};
class player1{
public:
player1();
~player1(){};
friend void fall(player1 num1,player2 num2,qipan num);
friend void input(player1 num1,player2 num2);
friend int judge_winner(player1 num1,player2 num2);
private:
int point1[line_num+1][line_num+1];
};
class player2{
public:
player2();
~player2(){};
friend void fall(player1 num1,player2 num2,qipan num);
friend void input(player1 num1,player2 num2,qipan num);
friend int judge_winner(player1 num1,player2 num2);
private:
int point2[line_num+1][line_num+1];
};
void input(player1 num1,player2 num2);
void fall(player1 num1,player2 num2,qipan num);
int judge_winner(qipan num,player1 num1,player2 num2);
void inputerror();
void display_winner(int);
void main()
{
int driver=DETECT,mode;
initgraph(driver,mode,"e:\tc30\bgi");
qipan num;
player1 num1;
player2 num2;
while(step--)
{
input(num1,num2,num);
fall(num1,num2,num);
if(judge_winner(num1,num2))
{
display_winner(k);
}
}
// getchar();
}
qipan::qipan(void)
{ int j,i;
char ch[2]="0";
setbkcolor(backcolor);
setcolor(linecolor);
for(i=0;i=line_num;i++)
{
line(left,top-i*d,left+line_num*d,top-i*d);
}
for(i=0;i=line_num;i++)
{
line(left+i*d,top,left+i*d,top-line_num*d);
}
for(i=0;i=line_num;i++)
{ if(*ch=='9'+1) *ch='a';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+i*d-2,top+r+3,ch);
(*ch)=(*ch)+1;
}
*ch='0';
for(i=0;i=line_num;i++)
{if(*ch=='9'+1) *ch='a';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left-r-10,top-d*i-3,ch);
(*ch)=(*ch)+1;
}
setcolor(defaultcolor);
for(i=0;i=line_num;i++)
{
for(j=0;j=line_num;j++)
point[i][j]=0;
}
}
void fall(player1 num1,player2 num2,qipan num)
{
int flag=k%2;
if(flag)
{ setcolor(player2_color);
num2.point2[x][y]=1;
num.point[x][y]=2;
circle(left+d*x,top-d*y,r);
setfillstyle(1,player2_color);
floodfill(left+d*x,top-d*y,player2_color);
}
else
{ num1.point1[x][y]=1;
num.point[x][y]=1;
setcolor(player1_color);
circle(left+d*x,top-d*y,r);
setfillstyle(1,player1_color);
floodfill(left+d*x,top-d*y,player1_color);
}
setcolor(defaultcolor);
}
void input(player1 num1,player2 num2,qipan num)
{ char xx,yy;
k++;
while(1)
{
regs1.h.ah=0;
xx=int86(22,®s1,®s1)-'0';
if(xx==('q'-'0')||xx==('Q'-'0'))
{ step=0;
return;
}
regs1.h.ah=0;
yy=int86(22,®s1,®s1)-'0';
if(yy==('q'-'0')||yy==('Q'-'0'))
{
step=0;
return ;
}
if(xx0||xxline_num)
{ inputerror();
continue;
}
if(yy0||yyline_num)
{inputerror();
continue;
}
if(num.point[xx][yy]==0)
{
break;
}
else
{
inputerror();
continue;
}
}
x=(int)xx;
y=(int)yy;
setcolor(backcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,"Input error");
setcolor(defaultcolor);
}
player1::player1()
{
int i,j;
for(i=0;i=line_num;i++)
{
for(j=0;j=line_num;j++)
point1[i][j]=0;
}
}
player2::player2()
{ int i,j;
for(i=0;i=line_num;i++)
{
for(j=0;j=line_num;j++)
point2[i][j]=0;
}
}
void inputerror(void)
{ setcolor(error_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,"Input error");
setcolor(defaultcolor);
}
int judge_winner(player1 num1,player2 num2)
{
int count=0,m=0,n=0,a=0,b=0,xx0,yy0;
int flag=k%2;
xx0=x; yy0=y;
if(!flag)
{ //left ------- right
while(xx0=1m4) {xx0--;m++;}
while(n9xx0=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up ------ down
count=0; xx0=x; m=0; n=0;
while(yy0=1m4){yy0--;m++;}
while(n9yy0=line_num)
{
if(num1.point1[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up ----- right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0=1m4){ xx0--; a++; m++;} m=0;
while(yy0=line_numm4){ yy0++; b++; m++;}
if(a=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0=line_numyy0=0n9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0--;
n++;
}
//right up ----- left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0while(yy0if(a=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0=0yy0=0n9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0--;
yy0--;
n++;
}
//no winer
return 0;
}
else
{
//left ------- right
while(xx0=1m4) {xx0--;m++;}
while(n9xx0=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up ------ down
count=0; xx0=x; m=0; n=0;
while(yy0=1m4){yy0--;m++;}
while(n9yy0=line_num)
{
if(num2.point2[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up ----- right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0=1m4){ xx0--; a++; m++;} m=0;
while(yy0=line_numm4){ yy0++; b++; m++;}
if(a=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0=line_numyy0=0n9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0--;
n++;
}
//right up ----- left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0while(yy0if(a=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0=0yy0=0n9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0--;
yy0--;
n++;
}
//no winer
return 0;
}
}
void display_winner(int k)
{
int flag=k%2;
if(!flag)
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+d*2,top+40,"Red is winner");
setcolor(defaultcolor);
step=0;
getchar();
}
else
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+2*d,top+40,"White is winner");
setcolor(defaultcolor);
step=0;
}
}