This repository has been archived by the owner on Feb 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
92 lines (79 loc) · 2.34 KB
/
Game.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
using System;
using System.Drawing;
using Console_Battleship.Enums;
namespace Console_Battleship
{
public class Game
{
public Player PlayerA { get; set; }
public Player PlayerB { get; set; }
private GameMode GameMode { get; set; }
public Game(GameMode gameMode)
{
GameMode = gameMode;
switch (gameMode)
{
case GameMode.PlayerVsComputer:
PlayerA = new Player("Player");
PlayerB = new NpcPlayer("Computer");
break;
default:
PlayerA = new NpcPlayer("Computer1");
PlayerB = new NpcPlayer("Computer2");
break;
}
PlayerA.PlaceShips();
PlayerB.PlaceShips();
}
private void Round()
{
Point shotPossition = PlayerA.FireShot();
ShotResult result = PlayerB.ProcessShot(shotPossition);
PlayerA.ProcessShotResult(shotPossition, result);
if (!PlayerB.HasLost)
{
shotPossition = PlayerB.FireShot();
result = PlayerA.ProcessShot(shotPossition);
PlayerB.ProcessShotResult(shotPossition, result);
}
}
public void PlayGame()
{
switch (GameMode)
{
case GameMode.PlayerVsComputer:
PlayerVsComputer();
break;
default:
ComputerVsComputer();
break;
}
PlayerA.ShowBoards();
PlayerB.ShowBoards();
if (PlayerA.HasLost)
{
Console.WriteLine(PlayerB.Name + " has won the game!");
}
else if (PlayerB.HasLost)
{
Console.WriteLine(PlayerA.Name + " has won the game!");
}
}
private void PlayerVsComputer()
{
PlayerA.ShowBoards();
while (!PlayerA.HasLost && !PlayerB.HasLost)
{
Round();
PlayerA.ShowBoards();
}
}
private void ComputerVsComputer()
{
while (!PlayerA.HasLost && !PlayerB.HasLost)
{
Round();
}
}
}
}