locked
C# tic tac toe RRS feed

  • Question

  • How would i create an unbeatable level 5 AI? how would the pseudo code look like? 
    Friday, December 19, 2014 5:06 PM

Answers

All replies

  • What does "level 5" mean?
    Friday, December 19, 2014 5:40 PM
  • unbeatable level
    Friday, December 19, 2014 6:15 PM
  • My understanding of gaming AI is rather slight.

    But

    As I understand it. some of them use self changing code.

    They learn basically.
    It all seemed very clever when my mate was explaining it.

    We were in the pub at the time though.


    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML

    Friday, December 19, 2014 6:33 PM
  • A tic-tac-toe board only has nine spaces. There are only so many moves that can be made. I'm not sure that an "unbeatable" AI is viable in something as simple as tic-tac-toe.

    Friday, December 19, 2014 7:04 PM
  • The game tree for tic tac toe is solved.

    The pseudocode for the strategy is actually on Wikipedia.

    Here's an amusing illustration of the game tree:  http://xkcd.com/832/

    There's also a solution over at StackOverflow.

    • Marked as answer by Kristin Xie Monday, December 29, 2014 9:33 AM
    Friday, December 19, 2014 7:25 PM
  • Sorry for late reply.

    Please review attached source code.

    Rename Form Name 

    For all button should have only one event.Please go through link for more details.

    https://www.youtube.com/watch?v=p3gYVcggQOU

    ----------------------------------------------------------------------------------------------------------------------

    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 Tic_Tac_Too
    {
        public partial class Form1 : Form
        {
            bool turn = true;
            int turncount = 0;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button_click(object sender, EventArgs e)
            {
                try
                {
                    Button b = (Button)sender;
                    if (turn)
                        b.Text = "X";
                    else
                        b.Text = "O";
                    turn = !turn;
                    b.Enabled = false;
                    turncount++;
                    CheckWinner();
                }
                catch
                { }
            }
            private void CheckWinner()
            {
                bool IsThereWinner = false;
                //Check Horizantal Match
                if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
                    IsThereWinner = true;
                else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
                    IsThereWinner = true;
                else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
                    IsThereWinner = true;
                //Check Vertical Match
                else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
                    IsThereWinner = true;
                else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
                    IsThereWinner = true;
                else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
                    IsThereWinner = true;
                //Check Dignoal Match
                //Check Vertical Match
                else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
                    IsThereWinner = true;
                else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
                    IsThereWinner = true;
                if (IsThereWinner)
                {
                    string winner = "";
                    if (turn)
                        winner = "0";
                    else
                        winner = "X";
                    DisableButton();
                    MessageBox.Show("Winner is : " + winner + " party time!!!", "You are winner!!!");
                }
                else
                {
                    if (turncount == 9)
                        MessageBox.Show("Match Draw!!", "Game is over");
                }
            }
            private void DisableButton()
            {
                try
                {
                    foreach (Control C in Controls)
                    {
                        Button B = (Button)C;
                        B.Enabled = false;
                    }
                }
                catch { }
            }
            private void Help_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Please make your mark Horizontal Or Vertical Or Digonal !!", "Game Winning Rules!!");
            }
    
            private void Application_Exit(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void Form1_DoubleClick(object sender, EventArgs e)
            {
    
            }
    
            private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
            {
                turn = true;
                turncount = 0;
                try
                {
                    foreach (Control C in Controls)
                    {
                        Button B = (Button)C;
                        B.Enabled = true;
                        B.Text = "";
                    }
                }
                catch { }
            }
    
    
        }
    }
    

    Button Name will be

    A1,A2,A3 && B1.B2.B3 && C1.C2.C3.

    Monday, August 3, 2015 8:07 PM