Ich hatte das jetzt noch ein Neuronal Netz mit rein programmiert ich hoffe dich stört das nicht.
C#-Codeusing 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;
}
}
}
C#-Codeusing 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);
}
}
}
C#-Codeusing 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());
}
}
}
}
C#-Codeusing 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;
}
}
}
C#-Codeusing 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();
}
}
}
}
C#-Codeusing 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;
}
}
}
C#-Codeusing 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());
}
}
}