|
|
|
อยากได้ Code หรือ แนวคิดในการเขียนเกมส์ OX โดยใช้ for และ array |
|
|
|
|
|
|
|
ไม่รู้ว่าคุณจะสร้างจากอะไร เอา checkbox ไปประยุกเอาน่ะ
Code (C#)
CheckBox[,] _chk = new CheckBox[20,20];
public Form1()
{
InitializeComponent();
CreateChk();
}
void CreateChk()
{
for (int iy = 1; iy <= 3; iy++)
{
for (int ix = 1; ix <= 3; ix++)
{
_chk[ix, iy] = new CheckBox();
_chk[ix, iy].Name = "_chk" + ix + iy;
_chk[ix, iy].Text = "chk" + ix + iy;
_chk[ix, iy].Location = new Point((10 + ((iy-1) * 140)), (10 + (ix * 50)));
this._chk[ix, iy].CheckStateChanged += new EventHandler(Form1_CheckStateChanged);
this.panel1.Controls.Add(_chk[ix, iy]);
}
}
}
void Form1_CheckStateChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
try
{
for (int iy = 1; iy <= 3; iy++)
{
for (int ix = 1; ix <= 3; ix++)
{
//กรณีแนวตั้ง
if (_chk[ix, iy].Checked)
{
if ((ix + 1) < 3)
{
if (_chk[ix + 1, iy].Checked)
{
if((ix+2) <= 3)
{
if (_chk[ix + 2, iy].Checked)
{
MessageBox.Show("ตั้ง");
}
}
}
}
}
//กรณีแนวนอน
if (_chk[ix, iy].Checked)
{
if ((iy + 1) < 3)
{
if (_chk[ix, iy + 1].Checked)
{
if ((iy + 2) <= 3)
{
if (_chk[ix, iy + 2].Checked)
{
MessageBox.Show("นอน");
}
}
}
}
}
//กรณีแนวทแยงขวาลง
if (_chk[ix, iy].Checked)
{
if ((iy + 1) <= 3 && (ix + 1) <= 3)
{
if (_chk[ix + 1, iy + 1].Checked)
{
if ((iy + 2) <= 3 && (ix + 2) <= 3)
{
if (_chk[ix + 2, iy + 2].Checked)
{
MessageBox.Show("ทะแยงขวาลง");
}
}
}
}
}
//กรณีแนวทแยงซ้ายลง
if (_chk[ix, iy].Checked)
{
if ((ix - 1) >= 1 && (iy + 1) <= 3)
{
if (_chk[ix - 1, iy + 1].Checked)
{
if ((ix - 2) >= 1 && (iy + 2) <= 3)
{
if (_chk[ix - 2, iy + 2].Checked)
{
MessageBox.Show("ทะแยงซ้ายลง");
}
}
}
}
}
}
}
}catch(Exception){
MessageBox.Show("Error");
}
}
|
|
|
|
|
Date :
2011-04-26 23:30:47 |
By :
hamzter |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Form1.Designer.cs
namespace TicTacToe
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 75);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(93, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 75);
this.button2.TabIndex = 0;
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(174, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 75);
this.button3.TabIndex = 0;
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(12, 93);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 75);
this.button4.TabIndex = 0;
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(93, 93);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 75);
this.button5.TabIndex = 0;
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(174, 93);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 75);
this.button6.TabIndex = 0;
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(12, 175);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 75);
this.button7.TabIndex = 0;
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(93, 175);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(75, 75);
this.button8.TabIndex = 0;
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(174, 175);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(75, 75);
this.button9.TabIndex = 0;
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(260, 262);
this.Controls.Add(this.button4);
this.Controls.Add(this.button5);
this.Controls.Add(this.button7);
this.Controls.Add(this.button8);
this.Controls.Add(this.button9);
this.Controls.Add(this.button6);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TicTacToe
{
public partial class Form1 : Form
{
private string[,] gameGrid;
private int count;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
NewGame();
}
private void NewGame()
{
gameGrid = new string[3, 3] { { string.Empty, string.Empty, string.Empty }, { string.Empty, string.Empty, string.Empty }, { string.Empty, string.Empty, string.Empty } };
count = 0;
button1.Text = string.Empty;
button2.Text = string.Empty;
button3.Text = string.Empty;
button4.Text = string.Empty;
button5.Text = string.Empty;
button6.Text = string.Empty;
button7.Text = string.Empty;
button8.Text = string.Empty;
button9.Text = string.Empty;
}
private bool CheckMatch()
{
for (int i = 0; i < 3; i++)
{
if (gameGrid[i, 0] != string.Empty && gameGrid[i, 1] != string.Empty && gameGrid[i, 2] != string.Empty && gameGrid[i, 0] == gameGrid[i, 1] && gameGrid[i, 1] == gameGrid[i, 2])
return true;
}
for (int i = 0; i < 3; i++)
{
if (gameGrid[0, i] != string.Empty && gameGrid[1, i] != string.Empty && gameGrid[2, i] != string.Empty && gameGrid[0, i] == gameGrid[1, i] && gameGrid[1, i] == gameGrid[2, i])
return true;
}
if (gameGrid[0, 0] != string.Empty && gameGrid[1, 1] != string.Empty && gameGrid[2, 2] != string.Empty && gameGrid[0, 0] == gameGrid[1, 1] && gameGrid[1, 1] == gameGrid[2, 2])
return true;
if (gameGrid[0, 2] != string.Empty && gameGrid[1, 1] != string.Empty && gameGrid[2, 0] != string.Empty && gameGrid[0, 2] == gameGrid[1, 1] && gameGrid[1, 1] == gameGrid[2, 0])
return true;
if (count > 8)
{
DialogResult result = MessageBox.Show("Draw Game");
if (result == System.Windows.Forms.DialogResult.OK)
NewGame();
}
return false;
}
private void GameOver()
{
DialogResult result = MessageBox.Show(((count - 1) % 2 == 0) ? "[O] Win!!!" : "[X] Win!!!");
if (result == System.Windows.Forms.DialogResult.OK)
NewGame();
}
private void button1_Click(object sender, EventArgs e)
{
if (gameGrid[0, 0] == string.Empty)
{
button1.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[0, 0] = button1.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button2_Click(object sender, EventArgs e)
{
if (gameGrid[0, 1] == string.Empty)
{
button2.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[0, 1] = button2.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button3_Click(object sender, EventArgs e)
{
if (gameGrid[0, 2] == string.Empty)
{
button3.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[0, 2] = button3.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button4_Click(object sender, EventArgs e)
{
if (gameGrid[1, 0] == string.Empty)
{
button4.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[1, 0] = button4.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button5_Click(object sender, EventArgs e)
{
if (gameGrid[1, 1] == string.Empty)
{
button5.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[1, 1] = button5.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button6_Click(object sender, EventArgs e)
{
if (gameGrid[1, 2] == string.Empty)
{
button6.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[1, 2] = button6.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button7_Click(object sender, EventArgs e)
{
if (gameGrid[2, 0] == string.Empty)
{
button7.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[2, 0] = button7.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button8_Click(object sender, EventArgs e)
{
if (gameGrid[2, 1] == string.Empty)
{
button8.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[2, 1] = button8.Text;
count++;
}
if (CheckMatch())
GameOver();
}
private void button9_Click(object sender, EventArgs e)
{
if (gameGrid[2, 2] == string.Empty)
{
button9.Text = (count % 2 == 0) ? "O" : "X";
gameGrid[2, 2] = button9.Text;
count++;
}
if (CheckMatch())
GameOver();
}
}
}
|
|
|
|
|
Date :
2011-04-27 10:03:17 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|