-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
200 lines (174 loc) · 7 KB
/
MainWindow.xaml.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Battleship.Model;
using System.Media;
namespace Battleship
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
GameController ctrl = new GameController(5);
List<Button> playerGridButtons = new List<Button>();
List<Button> computerGridButtons = new List<Button>();
bool gameRunning = true;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Place Ships
ctrl.Setup();
BuildGrid(playerPanel, playerGridButtons, false, false);
BuildGrid(computerPanel, computerGridButtons, true, true);
}
private void BuildGrid(StackPanel panel, List<Button> gridButtons, bool enabled, bool hidden)
{
for (int row = 0; row < 5; ++row)
{
StackPanel rowPanel = new StackPanel()
{
Orientation = Orientation.Horizontal
};
panel.Children.Add(rowPanel);
for (int col = 0; col < 5; ++col)
{
int btnNum = row * 3 + col;
Button b = MakeButton(enabled);
b.Tag = new Location(row, col);
//adding images to ships
Image img = new Image();
img.Height = b.Height;
img.Width = b.Width;
if(hidden)
img.Source = new BitmapImage(new Uri("Assets/water.png", UriKind.Relative));
else
{
if(ctrl.PlayerBoard.Cells[row, col] == CellType.Water) //this is biased to the player's view, not accurate to computer's board
img.Source = new BitmapImage(new Uri("Assets/water.png", UriKind.Relative));
else if(ctrl.PlayerBoard.Cells[row, col] == CellType.Ship)
img.Source = new BitmapImage(new Uri("Assets/ship.png", UriKind.Relative));
}
b.Content = img;
rowPanel.Children.Add(b);
gridButtons.Add(b);
}
}
}
// Note: The only buttons that can be clicked are those that are in the
// enemy's grid
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn == null) return;
Location loc = (Location)btn.Tag;
Image img = new Image();
img.Height = btn.Height;
img.Width = btn.Width;
Image cimg = new Image();
cimg.Height = btn.Height;
cimg.Width = btn.Width;
SoundPlayer player = new SoundPlayer();
if (gameRunning) {
// Update model to reflect player's attack
AttackResult playerAttackResult = ctrl.AttackComputer(loc);
if (playerAttackResult == AttackResult.Miss)
{
txtFeedback.Text = "you missed";
img.Source = new BitmapImage(new Uri("Assets/miss.jpg", UriKind.Relative));
player.SoundLocation = "Assets/miss.wav";
}
else if (playerAttackResult == AttackResult.Hit)
{
txtFeedback.Text = "Wooh! You hit a ship!!";
img.Source = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
player.SoundLocation = "Assets/hit.wav";
}
else if (playerAttackResult == AttackResult.Sink)
{
txtFeedback.Text = "You managed to sink a ship, good job.";
img.Source = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
player.SoundLocation = "Assets/sink.wav";
}
if (playerAttackResult != AttackResult.repeat) //ignore instead of throwing exception
{
btn.Content = img;
player.Load();
player.Play();
}
if (ctrl.IsGameOver())
{
player.SoundLocation = "Assets/win.wav";
player.Load();
player.Play();
gameRunning = false;
EndGame("You");
}
}
if (gameRunning) {
// Determine computer's response
ComputerAttackResult computerAttackResult = ctrl.AttackPlayer();
if (computerAttackResult.Result == AttackResult.Miss)
{
cimg.Source = new BitmapImage(new Uri("Assets/miss.jpg", UriKind.Relative));
}
else if (computerAttackResult.Result == AttackResult.Hit)
{
cimg.Source = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
}
else if (computerAttackResult.Result == AttackResult.Sink)
{
cimg.Source = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
}
for (int i = 0; i < playerGridButtons.Count; i++)
if ((playerGridButtons[i].Tag as Location).Column == computerAttackResult.Col && (playerGridButtons[i].Tag as Location).Row == computerAttackResult.Row)
{
playerGridButtons[i].Content = cimg;
break;
}
if (ctrl.IsGameOver())
{
player.SoundLocation = "Assets/lose.wav";
player.Load();
player.Play();
gameRunning = false;
EndGame("The computer");
}
}
}
void EndGame(string winner)
{
for (int i = 0; i < playerGridButtons.Count; i++)
{
computerGridButtons[i].IsEnabled = false;
playerGridButtons[i].IsEnabled = false;
}
MessageBox.Show(winner + " won the game!");
}
private Button MakeButton(bool enabled)
{
Button b = new Button();
b.BorderBrush = new SolidColorBrush(Colors.Black);
b.Width = 50;
b.Height = 50;
b.Margin = new Thickness(10);
b.IsEnabled = enabled;
b.Click += btn_Click;
return b;
}
}
}