-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
137 lines (123 loc) · 4.6 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySnake {
class Game {
Playfield playfield = new Playfield();
Player[] players = new Player[2];
PlayerControls player1Controls = new PlayerControls(ConsoleKey.DownArrow, ConsoleKey.UpArrow, ConsoleKey.LeftArrow, ConsoleKey.RightArrow, ConsoleKey.Spacebar);
PlayerControls player2Controls = new PlayerControls(ConsoleKey.S, ConsoleKey.W, ConsoleKey.A, ConsoleKey.D, ConsoleKey.Spacebar);
int difficulty = 0;
public void initGame() {
Console.CursorVisible = false;
Console.Clear();
generatePlayers();
playfield.generateLining(getAllSnakesPositions());
playfield.draw();
}
public void generatePlayers() {
Position playFieldSize = this.playfield.getPlayfieldSize();
this.players[0] = new Player(
"player1",
player1Controls,
new Position(playFieldSize.x + 3, 1),
this.playfield.firstPosition,
'o',
'O');
this.players[1] = new Player(
"player2",
player2Controls,
new Position(playFieldSize.x + 3, 3),
this.playfield.secondPosition,
'x',
'X');
}
public void start() {
chooseDifficulty();
initGame();
playfield.writeStartGameText();
if(Console.ReadKey(true).Key == ConsoleKey.Enter) {
playfield.clearStartGameText();
play();
}
}
void chooseDifficulty() {
Console.CursorVisible = true;
Console.WriteLine("Schwierigkeit auswählen (1-9)");
while(!(difficulty <= 9 && difficulty >= 1)) {
Console.WriteLine("Bitte eine Zahl zwischen 1 und 9 eingeben!");
try {
difficulty = Int32.Parse(Console.ReadLine());
}
catch(Exception ex) {
difficulty = 0;
}
}
}
void play() {
while(isGameOver() == false) {
processAllInputs();
checkNextPositions();
if(isGameOver() == false) {
foreach(Player player in this.players) {
player.snake.move();
}
}
System.Threading.Thread.Sleep(1000 / difficulty);
}
}
void checkNextPositions() {
foreach(Player player in this.players) {
Position nextPosition = player.snake.getNextPosition();
checkError(nextPosition, player);
checkEatLining(nextPosition, player);
}
}
void processAllInputs() {
while(Console.KeyAvailable) {
ConsoleKey pressedKey = Console.ReadKey(true).Key;
foreach(Player player in this.players) {
player.processUserInput(pressedKey);
}
}
}
bool isGameOver() {
foreach(Player player in this.players) {
if(player.gameOver) {
this.playfield.writeGameOverText(player.playerName);
return true;
}
}
return false;
}
bool checkEatLining(Position nextPosition, Player player) {
if(playfield.eatLining(nextPosition)) {
player.snake.grow();
player.score += difficulty;
player.writeScore();
playfield.generateLining(getAllSnakesPositions());
return true;
}
return false;
}
bool checkError(Position nextPosition, Player player) {
bool borderError = this.playfield.checkBorderError(nextPosition.x, nextPosition.y);
bool snakeError = player.checkSnakeError(getAllSnakesPositions());
if(borderError || snakeError) {
return player.gameOver = true;
}
return false;
}
LinkedList<Position> getAllSnakesPositions() {
LinkedList<Position> allSnakesPositions = new LinkedList<Position>();
foreach(Player player in this.players) {
foreach(Position pos in player.snake.getSnakePositions()) {
allSnakesPositions.AddLast(pos);
}
}
return allSnakesPositions;
}
}
}