-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
122 lines (116 loc) · 4.1 KB
/
Program.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
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
namespace WondevWomanUI
{
class MainClass : Form
{
private PictureBox box;
public static void Main (string[] args)
{
MainClass form = new MainClass ();
form.ShowDialog ();
}
static int size;
static int unitsPerPlayer;
public MainClass() {
box = new PictureBox { Parent = this, Dock = DockStyle.Fill };
size = int.Parse (Console.ReadLine ());
unitsPerPlayer = int.Parse (Console.ReadLine ());
scale = (xMax - xMin) / size;
unmarkedBoard = (Bitmap)Image.FromStream (Assembly.GetExecutingAssembly ().GetManifestResourceStream ("background.jpg"));
this.Width = 10 + unmarkedBoard.Width;
this.Height = 10 + unmarkedBoard.Height;
ReadState ();
box.Click += MakeMovement;
}
int xMin = 512, xMax = 1407, yMin = 165, scale;
List<Point> chain = new List<Point>();
private void MakeMovement(object sender, EventArgs e) {
Point cursor = box.PointToClient (Cursor.Position);
int gridX = (cursor.X - xMin) / scale;
int gridY = (cursor.Y - yMin) / scale;
if (gridX < 0 || gridX >= size || gridY < 0 || gridY >= size) {
box.Image.Dispose ();
box.Image = unmarkedBoard;
chain.Clear ();
return;
}
chain.Add (new Point (gridX, gridY));
Image bmp = new Bitmap (unmarkedBoard);
Graphics g = Graphics.FromImage (bmp);
foreach(Point p in chain)
g.DrawRectangle (Pens.BlueViolet, xMin + scale * p.X, yMin + scale * p.Y, scale, scale);
box.Image = bmp;
g.Dispose ();
if (chain.Count == 3) {
int index = myPos.IndexOf (chain [0]);
string dir1 = ("N S" [Math.Sign (chain [1].Y - chain [0].Y) + 1].ToString () + "W E" [Math.Sign (chain [1].X - chain [0].X) + 1]).Trim ();
string dir2 = ("N S" [Math.Sign (chain [2].Y - chain [1].Y) + 1].ToString () + "W E" [Math.Sign (chain [2].X - chain [1].X) + 1]).Trim ();
string command = " " + index + " " + dir1 + " " + dir2;
if (actions.Any (a => a.EndsWith (command)))
command = actions.First (a => a.EndsWith (command));
else
command = null;
chain.Clear ();
if (command != null) {
Console.WriteLine (command);
ReadState ();
}
}
}
List<Point> myPos = new List<Point> ();
List<string> actions = new List<string>();
private Bitmap unmarkedBoard;
public void ReadState() {
string[] inputs;
Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly ();
unmarkedBoard = (Bitmap)Image.FromStream (Assembly.GetExecutingAssembly ().GetManifestResourceStream ("background.jpg"));
Bitmap bmp = unmarkedBoard;
Graphics g = Graphics.FromImage (bmp);
for (int y = 0; y < size; y++)
{
string row = Console.ReadLine();
for (int x = 0; x < size; x++) {
if (row [x] >= '0' && row [x] <= '4') {
for (int c = '0'; c <= row [x]; c++) {
g.DrawImage (Image.FromStream (assembly.GetManifestResourceStream ("tile" + (c + 1 - '0') + ".png")), xMin + scale * x, yMin + scale * y, scale, scale);
}
}
}
}
myPos.Clear ();
for (int i = 0; i < unitsPerPlayer; i++)
{
inputs = Console.ReadLine().Split(' ');
int unitX = int.Parse(inputs[0]);
int unitY = int.Parse(inputs[1]);
myPos.Add (new Point (unitX, unitY));
g.DrawImage (Image.FromStream (assembly.GetManifestResourceStream ("player1.png")), xMin + scale * unitX, yMin + scale * unitY, scale, scale);
}
for (int i = 0; i < unitsPerPlayer; i++) {
inputs = Console.ReadLine ().Split (' ');
int otherX = int.Parse (inputs [0]);
int otherY = int.Parse (inputs [1]);
if (otherX != -1)
g.DrawImage (Image.FromStream (assembly.GetManifestResourceStream ("player2.png")), xMin + scale * otherX, yMin + scale * otherY, scale, scale);
}
int legalActions = int.Parse(Console.ReadLine());
if (legalActions == 0)
Console.WriteLine ("ACCEPT-DEFEAT");
actions.Clear ();
for (int i = 0; i < legalActions; i++)
{
actions.Add (Console.ReadLine ());
}
g.Dispose ();
if (box.Image != null)
box.Image.Dispose ();
box.Image = bmp;
unmarkedBoard = bmp;
}
}
}