-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiAssist.cs
138 lines (117 loc) · 4.65 KB
/
aiAssist.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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 SnakeEvo
{
public partial class showcase : Form
{
Snake testSnake;
Snake startSnake;
Timer gameLoop = new Timer();
Timer snakeLoop = new Timer();
int gameSpeed;
bool gameStarted = false;
public showcase()
{
InitializeComponent();
gameSpeed = 10;
gameLoop.Tick += new EventHandler(UpdateScreen);
snakeLoop.Tick += new EventHandler(UpdateSnake);
gameLoop.Interval = 1000 / 60;
snakeLoop.Interval = 1000 / gameSpeed;
gameLoop.Start();
snakeLoop.Start();
startButton.Enabled = false;
}
private void loadButton_Click(object sender, EventArgs e)
{
snakeSerial tempEkans;
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Load Snake";
fdlg.InitialDirectory = @"c:\temp\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
String fileName = fdlg.FileName;
// MessageBox.Show(fileName);
System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(snakeSerial));
System.IO.StreamReader file = new System.IO.StreamReader(fileName);
tempEkans = (snakeSerial)reader.Deserialize(file);
file.Close();
MessageBox.Show("Snake read!");
testSnake = new Snake(tempEkans.lifeTime);
testSnake.fitness = tempEkans.fitness;
testSnake.brain.whi.matrix = ArrayConvert.SingleToMulti(tempEkans.whi, tempEkans.whiRows, tempEkans.whiCols);
testSnake.brain.whh.matrix = ArrayConvert.SingleToMulti(tempEkans.whh, tempEkans.whhRows, tempEkans.whhCols);
testSnake.brain.woh.matrix = ArrayConvert.SingleToMulti(tempEkans.woh, tempEkans.wohRows, tempEkans.wohCols);
startButton.Enabled = true;
}
}
private void UpdateSnake(object sender, EventArgs e)
{
if (gameStarted)
{
startSnake.Look();
startSnake.SetVelocity();
startSnake.Move();
scoreLabel.Text = "Score: " + (startSnake.len-4) * 10;
if (startSnake.diedFromWall == true || startSnake.diedFromSelf == true)
{
gameStarted = false;
startButton.Text = "Restart";
}
}
}
private void UpdateScreen(object sender, EventArgs e)
{
pbCanvas.Invalidate();
}
private void Draw(Graphics canvas)
{
canvas.FillRectangle(new SolidBrush(Color.Orange), new Rectangle(startSnake.food.X * 8, startSnake.food.Y * 8, 8, 8));
for (int i = 0; i < startSnake.tail.Count; i++)
{
//Color snake_color = i == 0 ? Color.Red : Color.Black;
SnakePart currentPart = startSnake.tail[i];
canvas.FillRectangle(new SolidBrush(Color.Black), new Rectangle(currentPart.X * 8, currentPart.Y * 8, 8, 8));
}
canvas.FillRectangle(new SolidBrush(Color.Red), new Rectangle(startSnake.head.X * 8, startSnake.head.Y * 8, 8, 8));
}
private void pbCanvas_Paint(object sender, PaintEventArgs e)
{
if (gameStarted == true)
Draw(e.Graphics);
}
private void startButton_Click(object sender, EventArgs e)
{
if (!gameStarted)
{
gameStarted = true;
startButton.Text = "Stop";
startSnake = new Snake(testSnake.lifeTime);
startSnake = testSnake.Clone(testSnake.lifeTime);
startSnake.leftToLive = testSnake.leftTimeLive;
startSnake.leftTimeLive = testSnake.leftTimeLive;
startSnake.alive = true;
}
else
{
gameStarted = false;
startButton.Text = "Start";
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
gameSpeed = (int)numericUpDown1.Value;
snakeLoop.Interval = 1000 / gameSpeed;
}
}
}