创建一个窗口
设置两个panel控件,分别在属性中,dock里选择fill和右沾满
设置背景颜色,在右边的panel中设置两个button,分别命名为开始重来
在设置一个label控件,表示游戏状态
在设计中使用如下表示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 五子棋6
{
public partial class Form1 : Form
{
//游戏是否开始
private bool start;
//下的黑子还是白字
private bool type=true;
//是否为空,为空为0不为空为1,2
private int[,] ChessCheck = new int[11, 11];
public Form1()
{
InitializeComponent();
}
//初始化
private void InitializeThis()
{
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
ChessCheck[i, j] = 0;
}
start = false;
this.panel2.Invalidate();
type = true;
}
//画棋盘
private void panel2_Paint(object sender, PaintEventArgs e){
Graphics g = panel2.CreateGraphics();
ChessBoard.DrawCB(g);
}
//设置游戏控制界面的大小
private void panel1_Paint(object sender, PaintEventArgs e)
{
panel1.Size = new Size(MainSize.Wid - MainSize.CBWid-20, MainSize.Hei);
}
//按开始键后的结果
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "游戏开始";
start = true;
button1.Enabled = false;
button2.Enabled = true;
}
//按充值后的结果
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("确定要重新开始?", "提示", MessageBoxButtons.OK);
label1.Text = "游戏未开始";
start = false;
button1.Enabled = true;
button2.Enabled = false;
InitializeThis();
}
//设置游戏窗体大小
private void Form1_Load(object sender, EventArgs e)
{
start = false;
button1.Enabled = true;
button2.Enabled = false;
this.Width = MainSize.Wid ;
this.Height = MainSize.Hei;
}
//根据鼠标点击的位置画棋子
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
//把棋盘分为【11】【11】的数组
if (start){
int x1 = (e.X) / MainSize.CBGap;
int y1 = (e.Y) / MainSize.CBGap;
//判断此位置是否为空
if(ChessCheck[x1,y1]!=0){
return;
}
//下黑子还是白子
if (type){
ChessCheck[x1, y1] = 1;
}
else
{
ChessCheck[x1, y1] = 2;
}
//画棋子
Chess.DrawC(panel2, type, e);
//换颜色
type = !type;
//判断是否胜利
if (IsFull(ChessCheck)&&!BlackVictory(ChessCheck)&&WhiteVictory(ChessCheck)){
MessageBox.Show("平局");
InitializeThis();
label1.Text = "游戏尚未开始!";
}
if (BlackVictory(ChessCheck))
{
MessageBox.Show("黑方胜利");
InitializeThis();
label1.Text = "游戏尚未开始!";
}
if (WhiteVictory(ChessCheck ))
{
MessageBox.Show("白方胜利");
InitializeThis();
label1.Text = "游戏尚未开始!";
}
}
}
//是否满格
public bool IsFull(int[,] ChessCheck){
bool full = true;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (ChessCheck[i, j] == 0)
full = false;
return full;
}
}
return full;
}
//是否黑子胜利
public bool BlackVictory( int[,] ChessBack){
bool Win = false;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
if (ChessCheck[i,j]!=0)
{
//纵向判断
if (j<7)
{
if (ChessCheck[i, j] == 1 && ChessCheck[i, j + 1] == 1 && ChessCheck[i, j + 2] == 1 && ChessCheck[i, j + 3] == 1 && ChessCheck[i, j + 4] == 1)
{
return Win = true;
}
}
//横向判断
if (i<7)
{
if (ChessCheck[i, j] == 1 && ChessCheck[i + 4, j] == 1 && ChessCheck[i + 1, j] == 1 && ChessCheck[i + 2, j] == 1 && ChessCheck[i + 3, j] == 1)
{
return Win = true;
}
}
//斜向右下判断
if (i<7&&j<7)
{
if (ChessCheck[i, j] == 1 && ChessCheck[i + 1, j + 1] == 1 && ChessCheck[i + 2, j + 2] == 1 && ChessCheck[i + 3, j + 3] == 1 && ChessCheck[i + 4, j + 4] == 1)
{
return Win = true;
}
}
//斜向左下判断
if (i>=4&&j<7)
{
if (ChessCheck[i, j] == 1 && ChessCheck[i - 1, j + 1] == 1 && ChessCheck[i - 2, j + 2] == 1 && ChessCheck[i - 3, j + 3] == 1 && ChessCheck[i - 4, j + 4] == 1)
{
return Win = true;
}
}
}
}
}
return Win;
}
//同上
public bool WhiteVictory( int[,] ChessBack){
bool Win = false;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
if (ChessCheck[i, j] != 0)
{
if (j <7)
{
if (ChessCheck[i, j] == 2 && ChessCheck[i, j + 1] == 2 && ChessCheck[i, j + 2] == 1 && ChessCheck[i, j + 3] == 1 && ChessCheck[i, j + 4] == 1)
{
return Win = true;
}
}
if(i<7)
{
if (ChessCheck[i, j] == 1 && ChessCheck[i + 4, j] == 1 && ChessCheck[i + 1, j] == 1 && ChessCheck[i + 2, j] == 1 && ChessCheck[i + 3, j] == 1)
{
return Win = true;
}
}
if (i<7&&j<7)
{
if (ChessCheck[i, j] == 1 && ChessCheck[i + 1, j + 1] == 1 && ChessCheck[i + 2, j + 2] == 1 && ChessCheck[i + 3, j + 3] == 1 && ChessCheck[i + 4, j + 4] == 1)
{
return Win = true;
}
}
if(i>=4&&j<7)
if (ChessCheck[i, j] == 1 && ChessCheck[i - 1, j + 1] == 1 && ChessCheck[i - 2, j + 2] == 1 && ChessCheck[i - 3, j +3] == 1 && ChessCheck[i - 4, j +4] == 1)
{
return Win = true;
}
}
}
}
return Win;
}
}
}
一下是mainsize
namespace 五子棋6
{
class MainSize
{
//主框体大小
public static int Wid { get { return 550; } }
public static int Hei { get { return 500; } }
//棋盘大小
public static int CBWid { get { return 440; } }
public static int CBHei { get { return 440; } }
//棋盘宽度
public static int CBGap { get { return 40; } }
//棋子直径
public static int ChessRadious
{
get { return 34; }
}
}
}
以下是chessboard
class ChessBoard
{
public static void DrawCB(Graphics g)
{
int num = MainSize.CBWid / MainSize.CBGap - 1;
int gap = MainSize.CBGap;
g.Clear(Color.Gold);
for (int i = 0; i < num + 1; i++)
{
g.DrawLine(new Pen(Color.Black), 20, 20 + i * gap, 20 + num * gap, 20 + i * gap);
g.DrawLine(new Pen(Color.Black), 20 + gap * i, 20, 20 + i * gap, 20 + gap * num);
}
}
}
一下是chess
namespace 五子棋6
{
class Chess
{
public static void DrawC(Panel p, bool type, MouseEventArgs e)
{
Graphics g = p.CreateGraphics();
//确定棋子的中心位置
int x1 = (e.X ) / MainSize.CBGap;
int x2 = x1 * MainSize.CBGap + 20-17;
int y1 = (e.Y ) / MainSize.CBGap;
int y2 = y1 * MainSize.CBGap + 20-17;
if (type)
{
g.FillEllipse(new SolidBrush(Color.Black), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
}
else
{
g.FillEllipse(new SolidBrush(Color.White), x2, y2, MainSize.ChessRadious, MainSize.ChessRadious);
}
}
}
}
运行结果
我的新电脑中没有设置一个resources所以运行出错,但代码没事只是程序设置中出现问题,根据以上代码可运行,其实原来我想把背景设置成模板颜色于是添加了一个图片,但是图片画的时候位置出错,于是我就放弃了使用金色当背景,但是那个源程序还存在这行代码于是,运行出错。
以上是我的考试作业,供大家参考借鉴。