C# :: Aufgabe #196

3 Lösungen Lösungen öffentlich

Tic Tac Toe - Den Spieleklassiker grafisch darstellen

Fortgeschrittener - C# von Gelöschte Person - 18.12.2017 um 21:18 Uhr
Setze den allbekannten Klassiker Tic Tac Toe in einer Windows Form um. Es sollte Grafisch erkennbar sein, welche Felder schon belegt wurden. Am sollte ein Spieler Gewinnen so soll dies angezeigt und das Spiel anschließen neu gestartet werden. Sollten alle Felder gefüllt sein und kein Gegner feststehen, so sollte ein Unentschieden angezeigt, und anschließen das Spiel neu gestartet werden. Vorzugsweise sollte auch erkennbar sein, welcher Spieler gerade am Zug ist. Ein Computergegner ist nicht von Nöten, solltet ihr aber Lust darauf haben so könnt ihr auch das umsetzen.

Lösungen:

4 Kommentare
vote_ok
von Z3RP (1020 Punkte) - 21.12.2017 um 13:05 Uhr
Ich hatte das jetzt noch ein Neuronal Netz mit rein programmiert ich hoffe dich stört das nicht.

Quellcode ausblenden C#-Code
using NeuronalNetwork;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        Bitmap cross = new Bitmap(@"C:\Users\npreen.CARGOSOFT\source\repos\TicTacToe\TicTacToe\Images\cross.png");
        Bitmap circle = new Bitmap(@"C:\Users\npreen.CARGOSOFT\source\repos\TicTacToe\TicTacToe\Images\circle.png");
        int player = 1;
        bool game = true;
        Random rnd = new Random(DateTime.Now.Millisecond);
        Bot b1;

        PictureBox[] pBoxArr = new PictureBox[9];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            b1 = new Bot(rnd);
            pBoxArr[0] = pictureBox1;
            pBoxArr[1] = pictureBox2;
            pBoxArr[2] = pictureBox3;
            pBoxArr[3] = pictureBox4;
            pBoxArr[4] = pictureBox5;
            pBoxArr[5] = pictureBox6;
            pBoxArr[6] = pictureBox7;
            pBoxArr[7] = pictureBox8;
            pBoxArr[8] = pictureBox9;

            foreach (PictureBox pb in pBoxArr)
            {

                pb.BackgroundImage = null;

            }

            label1.Text = "Player 1 ist an der Reihe!";
            pictureBox10.BackgroundImage = cross;
            game = true;
            player = 1;

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            if (game)
            {
                if (player == 1)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = cross;
                        player = -1;
                        gameChange();
                    }
                }
                else
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }
        }


        private void gameChange()
        {

            label1.Update();

            if (player == 1)
            {
                label1.Text = "Player 1 ist an der Reihe!";
                pictureBox10.BackgroundImage = cross;
            }
            else
            {
                label1.Text = "Player 2 ist an der Reihe!";
                pictureBox10.BackgroundImage = circle;
            }

            if (pictureBox1.BackgroundImage == pictureBox2.BackgroundImage && pictureBox2.BackgroundImage == pictureBox3.BackgroundImage)
            {
                if (pictureBox1.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox1.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox1.BackgroundImage == pictureBox4.BackgroundImage && pictureBox4.BackgroundImage == pictureBox7.BackgroundImage)
            {
                if (pictureBox1.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox1.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox1.BackgroundImage == pictureBox5.BackgroundImage && pictureBox5.BackgroundImage == pictureBox9.BackgroundImage)
            {
                if (pictureBox1.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox1.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox2.BackgroundImage == pictureBox5.BackgroundImage && pictureBox5.BackgroundImage == pictureBox8.BackgroundImage)
            {
                if (pictureBox2.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox2.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox3.BackgroundImage == pictureBox6.BackgroundImage && pictureBox6.BackgroundImage == pictureBox9.BackgroundImage)
            {
                if (pictureBox3.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox3.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox4.BackgroundImage == pictureBox5.BackgroundImage && pictureBox5.BackgroundImage == pictureBox6.BackgroundImage)
            {
                if (pictureBox4.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox4.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox3.BackgroundImage == pictureBox5.BackgroundImage && pictureBox5.BackgroundImage == pictureBox7.BackgroundImage)
            {
                if (pictureBox3.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox3.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (pictureBox7.BackgroundImage == pictureBox8.BackgroundImage && pictureBox8.BackgroundImage == pictureBox9.BackgroundImage)
            {
                if (pictureBox7.BackgroundImage == cross)
                {
                    label1.Text = "Player 1 hat Gewonnen";
                    pictureBox10.BackgroundImage = cross;
                    game = false;
                }
                else if (pictureBox7.BackgroundImage == circle)
                {
                    label1.Text = "Player 2 hat Gewonnen";
                    pictureBox10.BackgroundImage = circle;
                    game = false;
                }
            }

            if (game)
            {
                game = false;
                foreach (PictureBox pb in pBoxArr)
                {
                    if (pb.BackgroundImage == null)
                    {
                        game = true;
                    }
                }

                if (!game)
                {
                    label1.Text = "Unentschieden";
                    pictureBox10.BackgroundImage = null;
                }
            }

            if (player == -1 && game)
            {
                b1.nn.resetOutputValue();

                for (int i = 0; i < pBoxArr.Length; i++)
                {
                    if (pBoxArr[i].BackgroundImage == cross)
                    {
                        b1.ineuArr[i].setValue(1);
                    }
                    else if (pBoxArr[i].BackgroundImage == circle)
                    {
                        b1.ineuArr[i].setValue(-1);
                    }
                    else if (pBoxArr[i].BackgroundImage == null)
                    {
                        b1.ineuArr[i].setValue(0);
                    }
                }

                OutputNeuron bestNeuron = new OutputNeuron();
                float bestValue = -99999999;

                for (int i = 0; i < b1.oneuArr.Length; i++)
                {
                    if (b1.oneuArr[i].getOutputValue() > bestValue && pBoxArr[i].BackgroundImage == null)
                    {
                        bestNeuron = b1.oneuArr[i];
                        bestValue = b1.oneuArr[i].getOutputValue();
                    }
                }

                for (int i = 0; i < pBoxArr.Length; i++)
                {
                    if (b1.oneuArr[i] == bestNeuron)
                    {
                        pBoxArr[i].BackgroundImage = circle;
                        player = 1;
                        gameChange();
                    }
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            foreach (PictureBox pb in pBoxArr)
            {
                pb.BackgroundImage = null;

            }

            label1.Text = "Player 1 ist an der Reihe!";
            pictureBox10.BackgroundImage = cross;
            game = true;
            player = 1;

            bool isLoaded = true;

            StreamReader sr = new StreamReader("guterBot.txt");
            char c = ';';
            String[] s = sr.ReadLine().Split(c);

            int c1 = 0;

            foreach (string s1 in s)
            {
                if(b1.mValues[c1] != Convert.ToInt32(s1))
                {
                    isLoaded = false;
                }
                c1++;
            }

            if (isLoaded)
            {
                label2.Text = "Bot geladen.";
            }
            else
            {
                label2.Text = "Bot nicht geladen.";
            }

            sr.Close();

            gameChange();
        }

        
        int bc = 0;
        private void saveBot_Click(object sender, EventArgs e)
        {
            while(File.Exists(@"C:\Users\npreen.CARGOSOFT\source\repos\TicTacToe\TicTacToe\bin\Debug\Bots\b" + bc + ".txt"))
            {
                bc++;
            }
            StreamWriter sw = new StreamWriter(@"C:\Users\npreen.CARGOSOFT\source\repos\TicTacToe\TicTacToe\bin\Debug\Bots\b"+bc+".txt");

            //sw.WriteLine("{0}", string.Join(";", mValues));
            sw.Close();
        }

        private void loadBot_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("guterBot.txt");
            char c = ';';
            String[] s = sr.ReadLine().Split(c);

            int c1 = 0;

            foreach (string s1 in s)
            {
                b1.mValues[c1] = Convert.ToInt32(s1);
                c1++;
            }

            b1.nn.createMesh(b1.mValues);

            sr.Close();
        }

        private void newBot_Click(object sender, EventArgs e)
        {
            b1 = new Bot(rnd);
            foreach (PictureBox pb in pBoxArr)
            {

                pb.BackgroundImage = null;

            }

            label1.Text = "Player 1 ist an der Reihe!";
            pictureBox10.BackgroundImage = cross;
            game = true;
            player = 1;
        }

    }
}


Quellcode ausblenden C#-Code
using NeuronalNetwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TicTacToe
{
    class Bot
    {
        Random rnd;
        public NeuronalNet nn;
        InputNeuron i1 = new InputNeuron();
        InputNeuron i2 = new InputNeuron();
        InputNeuron i3 = new InputNeuron();
        InputNeuron i4 = new InputNeuron();
        InputNeuron i5 = new InputNeuron();
        InputNeuron i6 = new InputNeuron();
        InputNeuron i7 = new InputNeuron();
        InputNeuron i8 = new InputNeuron();
        InputNeuron i9 = new InputNeuron();

        OutputNeuron o1 = new OutputNeuron();
        OutputNeuron o2 = new OutputNeuron();
        OutputNeuron o3 = new OutputNeuron();
        OutputNeuron o4 = new OutputNeuron();
        OutputNeuron o5 = new OutputNeuron();
        OutputNeuron o6 = new OutputNeuron();
        OutputNeuron o7 = new OutputNeuron();
        OutputNeuron o8 = new OutputNeuron();
        OutputNeuron o9 = new OutputNeuron();

        public InputNeuron[] ineuArr = new InputNeuron[9];

        public OutputNeuron[] oneuArr = new OutputNeuron[9];
        public float[] mValues;

        public Bot(Random rnd1)
        {
            rnd = rnd1;
            createNeuralNet();
        }

        public void createNeuralNet()
        {
            i1 = new InputNeuron();
            i2 = new InputNeuron();
            i3 = new InputNeuron();
            i4 = new InputNeuron();
            i5 = new InputNeuron();
            i6 = new InputNeuron();
            i7 = new InputNeuron();
            i8 = new InputNeuron();
            i9 = new InputNeuron();

            o1 = new OutputNeuron();
            o2 = new OutputNeuron();
            o3 = new OutputNeuron();
            o4 = new OutputNeuron();
            o5 = new OutputNeuron();
            o6 = new OutputNeuron();
            o7 = new OutputNeuron();
            o8 = new OutputNeuron();

            ineuArr[0] = i1;
            ineuArr[1] = i2;
            ineuArr[2] = i3;
            ineuArr[3] = i4;
            ineuArr[4] = i5;
            ineuArr[5] = i6;
            ineuArr[6] = i7;
            ineuArr[7] = i8;
            ineuArr[8] = i9;

            oneuArr[0] = o1;
            oneuArr[1] = o2;
            oneuArr[2] = o3;
            oneuArr[3] = o4;
            oneuArr[4] = o5;
            oneuArr[5] = o6;
            oneuArr[6] = o7;
            oneuArr[7] = o8;
            oneuArr[8] = o9;

            nn = new NeuronalNet();

            nn.addInputNeuron(i1);
            nn.addInputNeuron(i2);
            nn.addInputNeuron(i3);
            nn.addInputNeuron(i4);
            nn.addInputNeuron(i5);
            nn.addInputNeuron(i6);
            nn.addInputNeuron(i7);
            nn.addInputNeuron(i8);
            nn.addInputNeuron(i9);

            nn.addOutputNeuron(o1);
            nn.addOutputNeuron(o2);
            nn.addOutputNeuron(o3);
            nn.addOutputNeuron(o4);
            nn.addOutputNeuron(o5);
            nn.addOutputNeuron(o6);
            nn.addOutputNeuron(o7);
            nn.addOutputNeuron(o8);
            nn.addOutputNeuron(o9);

            mValues = new float[81];

            for (int i = 0; i < mValues.Length; i++)
            {
                //Thread.Sleep(10);
                mValues[i] = rnd.Next(0, 21) - 10;
            }

            nn.createMesh(mValues);

        }
    }
}




Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NeuronalNetwork
{
    class InputNeuron
    {
        private float value;
        private List<Link> links;

        public InputNeuron()
        {
            value = 0;
            this.links = new List<Link>();
        }

        public void addLink(Link l)
        {
            links.Add(l);
        }

        public float getValue()
        {
            return value;
        }

        public void setValue(float v)
        {
            value = v;

            foreach(Link l in links)
            {
                l.getOutput().setOutputValue(l.getCalcValue());
            }
        }
    }
}


Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NeuronalNetwork
{
    class Link
    {
        private InputNeuron input;
        private OutputNeuron output;
        private float value;
        private int calc;

        public Link(InputNeuron input, OutputNeuron output, float value, int calc)
        {
            this.input = input;
            this.output = output;
            this.value = value;
            this.calc = calc;
        }

        public OutputNeuron getOutput()
        {
            return output;
        }

        public float getCalcValue()
        {
            //if (calc == 0)
            //{
            //    return input.getValue() + value;
            //}
            //if (calc == 1)
            //{
            //    return input.getValue() - value;
            //}
            //if (calc == 2)
            //{
            //    return input.getValue() * value;
            //}
            return input.getValue() * value;
        }
    }
}


Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NeuronalNetwork
{
    class NeuronalNet
    {
        private List<InputNeuron> inputs;
        public List<OutputNeuron> outputs;
        Random rnd = new Random(DateTime.Now.Millisecond);

        public NeuronalNet()
        {
            this.inputs = new List<InputNeuron>();
            this.outputs = new List<OutputNeuron>();
        }

        public void addInputNeuron(InputNeuron i)
        {
            inputs.Add(i);
        }

        public void addOutputNeuron(OutputNeuron o)
        {
            outputs.Add(o);
        }

        public bool createMesh(float[] mValues)
        {
            int c = 0;
            if(mValues.Length != inputs.Count * outputs.Count)
            {
                return false;
            }
            foreach(InputNeuron i in inputs)
            {
                foreach(OutputNeuron o in outputs)
                {
                    i.addLink(new Link(i, o, mValues[c], rnd.Next(0, 3)));
                    c++;
                }
            }
            return true;
        }

        public void resetOutputValue()
        {
            foreach (OutputNeuron o in outputs)
            {
                o.resetValue();
            }
        }
    }
}


Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NeuronalNetwork
{
    class OutputNeuron
    {
        private float outputValue;

        public OutputNeuron()
        {
            this.outputValue = 0;
        }

        public void setOutputValue(float v)
        {
            outputValue += v;

        }

        public void resetValue()
        {
            outputValue = 0;
        }

        public float getOutputValue()
        {
            return outputValue;
        }
    }
}


Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TicTacToe
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
vote_ok
von Mexx (2370 Punkte) - 28.12.2017 um 16:20 Uhr
Form1.cs:
Quellcode ausblenden C#-Code
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Tic_Tac_Toe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tsText.Text = "Rot ist am Zug";
        }

        int[] spielfeld = new int[9];
        Color farbe = Color.Blue;
        private void PanelClick(object sender, EventArgs e)
        {
            tsText.Text = SpielerAmZug() + " ist am Zug";
            Panel p = (Panel)sender;
            int feld = Convert.ToInt16(p.Name.Replace("panelFeld", ""));
            feld--;
            if (spielfeld[feld] == 0)
            {
                Graphics g = p.CreateGraphics();
                if (farbe == Color.Blue)
                {
                    farbe = Color.Red;
                    g.DrawEllipse(new Pen(farbe, 10), p.DisplayRectangle);
                    spielfeld[feld] = 2;
                }
                else
                {
                    farbe = Color.Blue;
                    g.DrawLine(new Pen(farbe, 10), new Point(0, 0), new Point(p.Width, p.Height));
                    g.DrawLine(new Pen(farbe, 10), new Point(0, p.Height), new Point(p.Width, 0));
                    spielfeld[feld] = 1;
                }

                if (Prüfen())
                {                   
                    MessageBox.Show(SpielerAmZug() + " hat Gewonnen");
                }

                if (!FreiesFeld())
                {
                    MessageBox.Show("Unentschieden");
                }
            }
        }

        private bool FreiesFeld()
        {
            for (int i = 0; i < spielfeld.Length; i++)
            {
                if (spielfeld[i] == 0)
                    return true;
            }
            return false;
        }

        private string SpielerAmZug()
        {
            string spieler = "";
            if (farbe == Color.Blue)
            {
                spieler = "Blau";
            }
            else
            {
                spieler = "Rot";
            }
            return spieler;
        }

        private bool Prüfen()
        {
            if (spielfeld[0] == spielfeld[1] && spielfeld[0] == spielfeld[2] && spielfeld[0] != 0)
                return true;
            if (spielfeld[3] == spielfeld[4] && spielfeld[3] == spielfeld[5] && spielfeld[3] != 0)
                return true;
            if (spielfeld[6] == spielfeld[7] && spielfeld[6] == spielfeld[8] && spielfeld[6] != 0)
                return true;

            if (spielfeld[0] == spielfeld[3] && spielfeld[0] == spielfeld[6] && spielfeld[0] != 0)
                return true;
            if (spielfeld[1] == spielfeld[4] && spielfeld[1] == spielfeld[7] && spielfeld[1] != 0)
                return true;
            if (spielfeld[3] == spielfeld[5] && spielfeld[3] == spielfeld[8] && spielfeld[3] != 0)
                return true;

            if (spielfeld[0] == spielfeld[4] && spielfeld[0] == spielfeld[8] && spielfeld[0] != 0)
                return true;
            if (spielfeld[2] == spielfeld[4] && spielfeld[2] == spielfeld[6] && spielfeld[2] != 0)
                return true;

            return false;
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void neustartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
    }
}


Designer.cs:
Quellcode ausblenden C#-Code
namespace Tic_Tac_Toe
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code
        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.panelFeld1 = new System.Windows.Forms.Panel();
            this.panelFeld2 = new System.Windows.Forms.Panel();
            this.panelFeld3 = new System.Windows.Forms.Panel();
            this.panelFeld4 = new System.Windows.Forms.Panel();
            this.panelFeld5 = new System.Windows.Forms.Panel();
            this.panelFeld6 = new System.Windows.Forms.Panel();
            this.panelFeld7 = new System.Windows.Forms.Panel();
            this.panelFeld8 = new System.Windows.Forms.Panel();
            this.panelFeld9 = new System.Windows.Forms.Panel();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.menüToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.neustartToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.beendenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.tsText = new System.Windows.Forms.ToolStripStatusLabel();
            this.menuStrip1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panelFeld1
            // 
            this.panelFeld1.BackColor = System.Drawing.Color.White;
            this.panelFeld1.Location = new System.Drawing.Point(11, 35);
            this.panelFeld1.Name = "panelFeld1";
            this.panelFeld1.Size = new System.Drawing.Size(70, 70);
            this.panelFeld1.TabIndex = 0;
            this.panelFeld1.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld2
            // 
            this.panelFeld2.BackColor = System.Drawing.Color.White;
            this.panelFeld2.Location = new System.Drawing.Point(87, 35);
            this.panelFeld2.Name = "panelFeld2";
            this.panelFeld2.Size = new System.Drawing.Size(70, 70);
            this.panelFeld2.TabIndex = 1;
            this.panelFeld2.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld3
            // 
            this.panelFeld3.BackColor = System.Drawing.Color.White;
            this.panelFeld3.Location = new System.Drawing.Point(163, 35);
            this.panelFeld3.Name = "panelFeld3";
            this.panelFeld3.Size = new System.Drawing.Size(70, 70);
            this.panelFeld3.TabIndex = 1;
            this.panelFeld3.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld4
            // 
            this.panelFeld4.BackColor = System.Drawing.Color.White;
            this.panelFeld4.Location = new System.Drawing.Point(11, 111);
            this.panelFeld4.Name = "panelFeld4";
            this.panelFeld4.Size = new System.Drawing.Size(70, 70);
            this.panelFeld4.TabIndex = 1;
            this.panelFeld4.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld5
            // 
            this.panelFeld5.BackColor = System.Drawing.Color.White;
            this.panelFeld5.Location = new System.Drawing.Point(87, 111);
            this.panelFeld5.Name = "panelFeld5";
            this.panelFeld5.Size = new System.Drawing.Size(70, 70);
            this.panelFeld5.TabIndex = 1;
            this.panelFeld5.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld6
            // 
            this.panelFeld6.BackColor = System.Drawing.Color.White;
            this.panelFeld6.Location = new System.Drawing.Point(163, 111);
            this.panelFeld6.Name = "panelFeld6";
            this.panelFeld6.Size = new System.Drawing.Size(70, 70);
            this.panelFeld6.TabIndex = 1;
            this.panelFeld6.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld7
            // 
            this.panelFeld7.BackColor = System.Drawing.Color.White;
            this.panelFeld7.Location = new System.Drawing.Point(11, 187);
            this.panelFeld7.Name = "panelFeld7";
            this.panelFeld7.Size = new System.Drawing.Size(70, 70);
            this.panelFeld7.TabIndex = 1;
            this.panelFeld7.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld8
            // 
            this.panelFeld8.BackColor = System.Drawing.Color.White;
            this.panelFeld8.Location = new System.Drawing.Point(87, 187);
            this.panelFeld8.Name = "panelFeld8";
            this.panelFeld8.Size = new System.Drawing.Size(70, 70);
            this.panelFeld8.TabIndex = 1;
            this.panelFeld8.Click += new System.EventHandler(this.PanelClick);
            // 
            // panelFeld9
            // 
            this.panelFeld9.BackColor = System.Drawing.Color.White;
            this.panelFeld9.Location = new System.Drawing.Point(163, 187);
            this.panelFeld9.Name = "panelFeld9";
            this.panelFeld9.Size = new System.Drawing.Size(70, 70);
            this.panelFeld9.TabIndex = 1;
            this.panelFeld9.Click += new System.EventHandler(this.PanelClick);
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menüToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(245, 24);
            this.menuStrip1.TabIndex = 2;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // menüToolStripMenuItem
            // 
            this.menüToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.neustartToolStripMenuItem,
            this.beendenToolStripMenuItem});
            this.menüToolStripMenuItem.Name = "menüToolStripMenuItem";
            this.menüToolStripMenuItem.Size = new System.Drawing.Size(50, 20);
            this.menüToolStripMenuItem.Text = "Menü";
            // 
            // neustartToolStripMenuItem
            // 
            this.neustartToolStripMenuItem.Name = "neustartToolStripMenuItem";
            this.neustartToolStripMenuItem.Size = new System.Drawing.Size(120, 22);
            this.neustartToolStripMenuItem.Text = "Neustart";
            this.neustartToolStripMenuItem.Click += new System.EventHandler(this.neustartToolStripMenuItem_Click);
            // 
            // beendenToolStripMenuItem
            // 
            this.beendenToolStripMenuItem.Name = "beendenToolStripMenuItem";
            this.beendenToolStripMenuItem.Size = new System.Drawing.Size(120, 22);
            this.beendenToolStripMenuItem.Text = "Beenden";
            this.beendenToolStripMenuItem.Click += new System.EventHandler(this.beendenToolStripMenuItem_Click);
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsText});
            this.statusStrip1.Location = new System.Drawing.Point(0, 271);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(245, 22);
            this.statusStrip1.TabIndex = 3;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // tsText
            // 
            this.tsText.BackColor = System.Drawing.SystemColors.Control;
            this.tsText.ForeColor = System.Drawing.SystemColors.ControlText;
            this.tsText.Name = "tsText";
            this.tsText.Size = new System.Drawing.Size(118, 17);
            this.tsText.Text = "toolStripStatusLabel1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.Black;
            this.ClientSize = new System.Drawing.Size(245, 293);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.panelFeld9);
            this.Controls.Add(this.panelFeld8);
            this.Controls.Add(this.panelFeld7);
            this.Controls.Add(this.panelFeld6);
            this.Controls.Add(this.panelFeld5);
            this.Controls.Add(this.panelFeld4);
            this.Controls.Add(this.panelFeld3);
            this.Controls.Add(this.panelFeld2);
            this.Controls.Add(this.panelFeld1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Tic Tac Toe";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private System.Windows.Forms.Panel panelFeld1;
        private System.Windows.Forms.Panel panelFeld2;
        private System.Windows.Forms.Panel panelFeld3;
        private System.Windows.Forms.Panel panelFeld4;
        private System.Windows.Forms.Panel panelFeld5;
        private System.Windows.Forms.Panel panelFeld6;
        private System.Windows.Forms.Panel panelFeld7;
        private System.Windows.Forms.Panel panelFeld8;
        private System.Windows.Forms.Panel panelFeld9;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem menüToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem neustartToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem beendenToolStripMenuItem;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel tsText;
    }
}
vote_ok
von Manita112 (40 Punkte) - 20.01.2018 um 10:38 Uhr
Quellcode ausblenden C#-Code
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 TicTacToe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static int counter = 0;
        bool spieler = true;

        private void PButton(object sender, EventArgs e)
        {
            Button PButton = (Button)sender;
            if (spieler)
            {
                PButton.Text = "X";
                spieler = false;
            }
            else
            {
                PButton.Text = "O";
                spieler = true;
            }
            PButton.Enabled = false;
            counter++;
            gewinner();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void neuesSpielToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            counter = 0;
            spieler = true;
            foreach (Control c in Controls.OfType<Button>())
            {
                Button neu = (Button)c;
                neu.Enabled = true;
                neu.Text = "";
            }

        }

        private void gewinner()
        {
            if ((buttonA1.Text == buttonA2.Text) && (buttonA2.Text == buttonA3.Text) && (!buttonA1.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }

            }
            else if ((buttonB1.Text == buttonB2.Text) && (buttonB2.Text == buttonB3.Text) && (!buttonB1.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }

            }
            else if ((buttonC1.Text == buttonC2.Text) && (buttonC2.Text == buttonC3.Text) && (!buttonC1.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }
            }

            else if ((buttonA1.Text == buttonB1.Text) && (buttonB1.Text == buttonC1.Text) && (!buttonA1.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }

            }
            else if ((buttonA2.Text == buttonB2.Text) && (buttonB2.Text == buttonC2.Text) && (!buttonA2.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }

            }
            else if ((buttonA3.Text == buttonB3.Text) && (buttonB3.Text == buttonC3.Text) && (!buttonA3.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }
            }

            else if ((buttonA1.Text == buttonB2.Text) && (buttonB2.Text == buttonC3.Text) && (!buttonA1.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }
            }
            else if ((buttonA3.Text == buttonB2.Text) && (buttonB2.Text == buttonC1.Text) && (!buttonA3.Enabled))
            {
                nachgewinn();
                if (spieler)
                {
                    MessageBox.Show("O hat gewonnen");
                }
                else
                {
                    MessageBox.Show("X hat gewonnen");
                }
            }
            else if (counter == 9)
            {
                MessageBox.Show("Unentschieden!");
            }
        }

        private void nachgewinn()
        {
            foreach (Control c in Controls.OfType<Button>())
            {
                Button PButton = (Button)c;
                PButton.Enabled = false;

            }
        }
    }
}

1800413

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.