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 WindowsFormsApplication1 { public partial class Form1 : Form { private int index; private int players; private int level; // Buttons table for indexed button handling Button[] mButtons = new Button[9]; // Used Class definitions CheckWinner winnerClass; ArtificialIntelligence aiClass; public Form1() { InitializeComponent(); // Request player mode (1 or 2 players) if (MessageBox.Show( "Select 2 player mode?", "Game mode:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { players = 2; } else { players = 1; // Request inteligence mode (Artificial- inteligence/Nonsense) if (MessageBox.Show("Select Artificial inteligence?", "Game Difficulty Level:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { level = 2; //Artificial Intelligence } else { level = 1; //Artificial Nonsense } } // Initialize Form for game start intialize(); // Initialize used classes winnerClass = new CheckWinner(); winnerClass.Initialize(mButtons); aiClass = new ArtificialIntelligence(); aiClass.Initialize(mButtons); } private void button1_Click(object sender, EventArgs e) { int status; Button b = sender as Button; // Button already used if ( 0 == b.Text.Length ) { if ( 0 == index % 2) { b.Text = "X"; } else { b.Text = "O"; } index++; status = checkWinner(); if (0 == status && 1 == players) { // set computer move if ( 1 == level){ artificialNonsense(); } else{ artificialIntelligence(); } status = checkWinner(); } } } private void intialize() { index=1; // Set real buttons to button table mButtons[0] = button1; mButtons[1] = button2; mButtons[2] = button3; mButtons[3] = button4; mButtons[4] = button5; mButtons[5] = button6; mButtons[6] = button7; mButtons[7] = button8; mButtons[8] = button9; // Clear button name used for X and O (player) CleanScreen(); // Select X as first player label7.Text = "Turn: Player X"; } public void artificialIntelligence() { // Use real logic // Find 2 pair lines -> no -> set random -> otherwise block // check rows index++; // next turn is human player aiClass.CreateMove(); } public void artificialNonsense() { int button; button = aiClass.CreateRandomMove(); index++; // next turn is human player } public int checkWinner() { int ret = 0; ret = winnerClass.getWinnerStatus(); // Check game state if(1 == ret){ HandleRetryExit("Winner is X."); } else if(2 == ret) { HandleRetryExit("Winner is O."); } else if (10 == index){ HandleRetryExit("The game is draw."); ret = 3; } return ret; } public void HandleRetryExit(String caption) { // Create Dialog, add text and image, associate a sound var result = MessageBox.Show( "Try again ?", caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if(result == DialogResult.No) { Application.Exit(); // Close program } else { // Start again CleanScreen(); } } public void CleanScreen() { for (int i = 0; i < 9; i++) { mButtons[i].Text = ""; } index=1; } } /* AI CLASS */ public class ArtificialIntelligence { private Button[] btnTable; private Random rnd; private bool classInitialized = false; public void Initialize( Button[] table ) { btnTable = table; // initialize random class rnd = new Random(); classInitialized = true; } public int CreateMove() { int ret = 0; if (classInitialized != true) { throw new Exception("Class Init is missing"); } else { ret = CheckRows(btnTable); if (0 == ret) { ret = CheckColumns(btnTable); } if (0 == ret) { ret = CheckCrossLines(btnTable); } // no need block, use random logic if (0 == ret) { ret = CreateRandomMove(); } } return ret; } public int CreateRandomMove() { int button = 0; if (true != classInitialized) { throw new Exception("Class Init is missing"); } else { // randomize turns, select next empty randomly selected button. while (getButtonTextLength(btnTable[button = rnd.Next(0, 9)]) != 0) { ; } setButtonText(btnTable[button], "X"); } return button + 1; } private int CreateLineMove(Button btn1, Button btn2, Button btn3) { int ret = 0; ret = CreateLineMoveSign(btn1, btn2, btn3, "O"); if (0 == ret) { ret = CreateLineMoveSign(btn1, btn2, btn3, "X"); } return ret; } private int CreateLineMoveSign(Button btn1, Button btn2, Button btn3, String sign) { // Block Opponents attack if (0 == getButtonTextLength(btn1) && getButtonText(btn2) == sign) { // buttons 2 and 3 used if (getButtonText(btn2) == getButtonText(btn3)) { setButtonText(btn1, "X"); return 1; } } if (0 == getButtonTextLength(btn2) && getButtonText(btn1) == sign) { // buttons 1 and 3 used if (getButtonText(btn1) == getButtonText(btn3)) { setButtonText(btn2, "X"); return 2; } } if (0 == getButtonTextLength(btn3) && getButtonText(btn2) == sign) { // buttons 1 and 2 used if (getButtonText(btn2) == getButtonText(btn1)) { setButtonText(btn3, "X"); return 3; } } return 0; } private int CheckRows(Button[] table) { int ret = 0; // check rows for (int i = 0; i < 3 && 0 == ret; i++) { ret = CreateLineMove(table[0 + (i * 3)], table[1 + (i * 3)], table[2 + (i * 3)]); } return ret; } private int CheckColumns(Button[] table) { int ret = 0; // check columns for (int i = 0; i < 3 && 0 == ret; i++) { ret = CreateLineMove(table[0 + i], table[3 + i], table[6 + i]); } return ret; } private int CheckCrossLines(Button[] table) { int ret = 0; // check cross line \ ret = CreateLineMove(table[0], table[4], table[8]); // check cross line / if (0 == ret) { ret = CreateLineMove(table[2], table[4], table[6]); } return ret; } private int getButtonTextLength(Button btn) { return btn.Text.Length; } private string getButtonText(Button btn) { return btn.Text; } private void setButtonText(Button btn, string text) { btn.Text = text; } } /* CHECK WINNER CLASS */ public class CheckWinner { private Button[] btnTable; private bool classInitialized = false; public void Initialize(Button[] table) { btnTable = table; classInitialized = true; } public int getWinnerStatus() { int ret = 0; if (true != classInitialized) { throw new Exception("Class Init is missing"); } else { ret = CheckRows(btnTable); if (0 == ret) { ret = CheckColumns(btnTable); } if (0 == ret) { ret = CheckCrossLines(btnTable); } } return ret; } private int CheckRows(Button[] table) { int ret = 0; // check rows for (int i = 0; i < 3 && 0 == ret; i++) { ret = CheckLine(table[0 + (i * 3)], table[1 + (i * 3)], table[2 + (i * 3)]); } return ret; } private int CheckColumns(Button[] table) { int ret = 0; // check columns for (int i = 0; i < 3 && 0 == ret; i++) { ret = CheckLine(table[0 + i], table[3 + i], table[6 + i]); } return ret; } private int CheckCrossLines(Button[] table) { int ret = 0; // check cross line \ ret = CheckLine(table[0], table[4], table[8]); // check cross line / if (0 == ret) { ret = CheckLine(table[2], table[4], table[6]); } return ret; } private int CheckLine(Button btn1, Button btn2, Button btn3) { int ret = 0; if (getButtonText(btn1) == getButtonText(btn2) && getButtonText(btn1) == getButtonText(btn3) ) { if (getButtonText(btn1) == "X") { ret = 1; } else if (getButtonText(btn1) == "O") { ret = 2; } } return ret; } private string getButtonText(Button btn) { return btn.Text; } } }