-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestgame.dart
More file actions
531 lines (367 loc) · 14.9 KB
/
testgame.dart
File metadata and controls
531 lines (367 loc) · 14.9 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import "./components/deckgen.dart";
import "./components/shuffler.dart";
import "./components/croupier.dart";
import "./components/playerIO.dart";
import "dart:io";
import "components/cardpile.dart";
class Game{
List<List> teams = [];
int currentRound = 0;
late String manilha;
Game(this.teams, this.manilha);
@override
String toString() => "Round: $currentRound | Teams: $teams";
}
// im not exatcly proud of this but it's the only way i managed to make it work
// why doesnt this work?????? Process.runSync('cls', [], runInShell: true)
void clearTerminal(){
for(int i = 0; i < stdout.terminalLines; i++) {
stdout.writeln();
}
}
void displayHand(List<Card> hand){
print("Your hand:");
for(int i = 0; i < hand.length; i++){
Card currentCard = hand[i];
print(" $i | $currentCard");
}
print("----------------------------------------------");
}
// this will return the face of the manilha for the "card"
// "card" should be the first card on the shuffled deck AFTER the croupier dealt 3 cards to each player
String manilhaFinder(Card card){
// if the selected card is 3, AKA the strongest card
if(card.value == '3'){
return '4';
}else{
return (int.parse(card.value) + 1).toString();
}
}
// this solution is not pretty but I only remembered about manilhas now so i have to improvise
// on the actual game this will be improved
void manilhaUpdater(Game game){
// create a list with all players
// again, there are better ways to do this but I didnt plan ahead so this will do.
List<Player> players = [];
players.add(game.teams[0][0]);
players.add(game.teams[0][1]);
players.add(game.teams[1][0]);
players.add(game.teams[1][1]);
// go through every player, check if the have a manilha and if they do, update the trueValue of that card
// player loop
for(int p = 0; p < players.length; p++){
// current player's hand loop
for(int c = 0; c < players[p].hand.length; c++){
// if they have a manilha
if(players[p].hand[c].value == game.manilha){
players[p].hand[c].trueValue = 11;
}
// else do nothing
}
}
}
List<int> round(Game game){
int pointsWorth = 1;
// set all players' points to 0
game.teams[0][0].points = 0;
game.teams[0][1].points = 0;
game.teams[1][0].points = 0;
game.teams[1][1].points = 0;
String manilha = game.manilha;
print('|=========================|');
print('| New Round Starting... |');
print('|=========================|');
print('| Manilha: $manilha');
print('|-------------------------|');
stdout.write('Press enter to continue: ');
// detect key press
stdin.readLineSync();
clearTerminal();
// check if any of the teams have reached 2 points by checking the amount of points of the first player of that team
// when the while loop ends, a team will have won the round
while(game.teams[0][0].points < 2 && game.teams[1][0].points < 2){
List<Card> pile = [];
// amount of players in the game = amount of teams * amount of players per team
int amountOfPlayers = game.teams.length * game.teams[0].length;
int currentTeam = 0;
// this will run for amountOfPlayers times, each time it runs, one player makes a play
for(int i = 0; i < amountOfPlayers; i++){
clearTerminal();
// order will be: first player from team A -> first player from team B -> second player from team A -> second player from team B
Player currentPlayer = game.teams[currentTeam][(i/2).floor()];
String currentPlayerName = currentPlayer.name;
print("==========================================================");
print('New Play Starting... (Best of 3 plays wins the round)');
print("==========================================================");
print("It's $currentPlayerName turn.");
print("NO ONE ELSE SHOULD LOOK AT THE SCREEN NOW. ");
print("==========================================================");
stdout.write("Press enter when only $currentPlayerName is looking: ");
// detect key press
stdin.readLineSync();
clearTerminal();
print("==============================================");
print("It's $currentPlayerName's turn.");
print("==============================================");
var tempManilha = game.manilha;
print('Manilha: $tempManilha');
print("----------------------------------------------");
if(pile.length > 0){
Card topOfPile = pile[0];
print("Top of the pile: $topOfPile");
print("----------------------------------------------");
}
displayHand(currentPlayer.hand);
stdout.write("Choose a card to play by typing it's index: ");
String? inputCard = stdin.readLineSync();
int choosenCard;
// test if choosen card isnt null and is less than the hand's length
if(inputCard != null){
choosenCard = int.parse(inputCard);
}else{
choosenCard = 0;
}
// assign the current team as the owner of the choosen card for win check later
currentPlayer.hand[choosenCard].teamOwner = currentTeam;
// add card to pile
// if card is manilha, suit will be checked in case of a tie
if(currentPlayer.hand[choosenCard].value == game.manilha){
addToPile(pile, currentPlayer.hand[choosenCard], true);
// if card is NOT manilha, suit will NOT be checked in case of a tie
}else{
addToPile(pile, currentPlayer.hand[choosenCard], false);
}
// remove the choosen card from the hand
currentPlayer.hand.removeAt(choosenCard);
// alternate between the teams
if(currentTeam == 0){
currentTeam = 1;
}else{
currentTeam = 0;
}
} // play ends (2 wins for round win)
// assign points to the team that won the round
// check if there's a tie (if the first and second card of the pile, counting top-down, have the same face value and are not manilhas)
// there might be a better way to write this if but it's 1am and i'm not thinking straight
// if there's a tie, give one point to both teams
if(pile[0].trueValue == pile[1].trueValue && pile[0].trueValue != 11 && pile[1].trueValue != 11){
game.teams[0][0].point += 1;
game.teams[0][1].point += 1;
game.teams[1][0].point += 1;
game.teams[1][1].point += 1;
clearTerminal();
print('|===========================================|');
print('| Play Ended. |');
print('|-------------------------------------------|');
print('| THERE IS A TIE! |');
print('|===========================================|');
// if there ISN'T a tie, give a point to the winner
}else{
int winningTeam = pile[0].teamOwner;
// give one point to both players on that team
game.teams[winningTeam][0].points += 1;
game.teams[winningTeam][1].points += 1;
clearTerminal();
String winner1 = game.teams[winningTeam][0].name;
String winner2 = game.teams[winningTeam][1].name;
print('|===========================================|');
print('| Play Ended. |');
print('|-------------------------------------------|');
print('| Winner team: $winner1 and $winner2');
}
String team1 = game.teams[0][0].name + ' and ' + game.teams[0][1].name;
String team2 = game.teams[1][0].name + ' and ' + game.teams[1][1].name;
var team1Pts = game.teams[0][0].points;
var team2Pts = game.teams[1][0].points;
print('|===========================================|');
print('| First to 2 points wins. Points per team:');
print('| Team $team1 -> $team1Pts');
print('| Team $team2 -> $team2Pts');
print('|-------------------------------------------|');
stdout.write('Press enter to continue: ');
// detect key press
stdin.readLineSync();
clearTerminal();
} // round ends (12 wins for game win)
// string with the names of the players in each team
// String team1 = game.teams[0][0].name + ' and ' + game.teams[0][1].name;
// String team2 = game.teams[1][0].name + ' and ' + game.teams[1][1].name;
// count the points of each team
var team1Pts = game.teams[0][0].points;
var team2Pts = game.teams[1][0].points;
// var winnerTeam;
var winnerTeamIndex;
// if first team won
if(team1Pts > team2Pts){
// winnerTeam = team1;
winnerTeamIndex = 0;
// if second team won
}else{
// winnerTeam = team2;
winnerTeamIndex = 1;
}
// THIS IS NOT NEEDED ANYMORE AS THE ROUND ENDED TEXT IS PRINTED BY gameHandler()
// I WON'T DELETE THIS FOR NOW AS THE CODE HAS NOT BEEN PROPERLY TESTED YET.
// print('|===========================================|');
// print('| Round Ended |');
// print('|-------------------------------------------|');
// print('| Winner team: $winnerTeam');
// print('|-------------------------------------------|');
// print('| First to 2 play wins won the round.');
// print('| Team $team1 -> $team1Pts plays won.');
// print('| Team $team2 -> $team2Pts plays won.');
// print('|===========================================|');
// stdout.write('Press enter to continue: ');
// // detect key press
// stdin.readLineSync();
// clearTerminal();
// the return of round() is a list of two ints
// first int is the index of the winning team
// second int is the amount of points
return [winnerTeamIndex ,pointsWorth];
}
void gameHandler(){
// both teams start with zero points
// game ends when one reaches 12
List<int> pointsPerTeam = [0, 0];
// --------------
// setup begins
// --------------
// create players
// teams will be: p1 and p3 / p2 and p4
print('|=========================|');
print('| Create 4 players. |');
print('|=========================|');
print('');
// player1 -> team A
print('This player will be on team A [Player 1/4]');
Player p1 = createPlayer();
clearTerminal();
// player2 -> team B
print('This player will be on team B [Player 2/4]');
Player p2 = createPlayer();
clearTerminal();
// player3 -> team A
print('This player will be on team A [Player 3/4]');
Player p3 = createPlayer();
clearTerminal();
// player4 -> team B
print('This player will be on team B [Player 4/4]');
Player p4 = createPlayer();
clearTerminal();
// confirm the players
print('|=========================|');
print('| Confirm Players |');
print('|=========================|');
print(p1);
print(p2);
print(p3);
print(p4);
print('');
stdout.write('Press enter to continue: ');
// detect key press
stdin.readLineSync();
// -------------
// setup ends
// -------------
int pointsTeam0 = 0;
int pointsTeam01 = 0;
// each loop is a round
// while none of the teams have reached 12 points, start a new round
while(pointsTeam0 < 12 && pointsTeam01 < 12){
// create deck
List<Card> deck = deckgen(["4", "5", "6", "7", "10", "11", "12", "1", "2", "3"], ["Moles", "Espadas", "Copas", "Paus"]);
// shuffle deck
deck = shuffler(deck, 1);
// make sure all players have empty hands
p1.hand = [];
p2.hand = [];
p3.hand = [];
p4.hand = [];
// croupier
croupier([p1, p2, p3, p4], 3, deck);
// get manilha
String manilha = manilhaFinder(deck[0]);
deck.removeAt(0);
// create game
Game game = new Game([[p1, p3], [p2, p4]], manilha);
// check if any players have a manilha and if they do, update it's trueValue to 11
// before opening an issue, read the comments on the manilhaUpdater() function, thank you
manilhaUpdater(game);
clearTerminal();
// -------- !TO-DO! --------------------------------------------------------------------
// instead of starting a round, start a game via gameHandler()
// game will have part of the logic that is currently on this main function, as each game will have a different deck
// game will start a round and keep track of rounds won
// round will keep track of truco calls
// round should return the winning team and also the amount of points that team got
// -------- !TO-DO! --------------------------------------------------------------------
// start a round
// return value is [indexOfWinnerTeam, amountOfPoints]
List<int> latestRound = round(game);
// update the amount of points per team
pointsPerTeam[latestRound[0]] = latestRound[1];
// here, before a new round starts, print how many rounds each team won
var latestRoundPointsWorth = latestRound[1];
var latestWinnerTeamIndex = latestRound[0];
var latestWinnerTeamNames;
var team1Names = p1.name + ' and ' + p3.name;
var team2Names = p2.name + ' and ' + p4.name;
var team1Points = pointsPerTeam[0];
var team2Points = pointsPerTeam[1];
if(latestWinnerTeamIndex == 0){
latestWinnerTeamNames = team1Names;
}else{
latestWinnerTeamNames = team2Names;
}
print('|===========================================|');
print('| Round Ended |');
print('|-------------------------------------------|');
print('| $latestRoundPointsWorth points awarded to $latestWinnerTeamNames');
print('|-------------------------------------------|');
print('| First to 12 points wins the game!');
print('| Team $team1Names -> $team1Points points.');
print('| Team $team2Names -> $team2Points points.');
print('|===========================================|');
stdout.write('Press enter to continue: ');
// detect key press
stdin.readLineSync();
clearTerminal();
// this is the end of the rounds loop, if none of the teams have 12 points, it will run again
// if one of the teams got to 12 points, it will stop
}
var gameWinnerTeam;
String team1Names = p1.name + ' and ' + p3.name;
String team2Names = p2.name + ' and ' + p4.name;
int gamePointsTeam1 = pointsPerTeam[0];
int gamePointsTeam2 = pointsPerTeam[1];
// if first team won the game
if(pointsPerTeam[0] > pointsPerTeam[1]){
gameWinnerTeam = team1Names;
// if second team won the game
}else{
gameWinnerTeam = team2Names;
}
print('|===========================================|');
print('| Game Ended. |');
print('|-------------------------------------------|');
print('| Winner team: $gameWinnerTeam');
print('|-------------------------------------------|');
print('| First to 12 points won. Points per team:');
print('| Team $team1Names -> $gamePointsTeam1');
print('| Team $team2Names -> $gamePointsTeam2');
print('|===========================================|');
}
void main(){
print('|=========================|');
print('| E-Truco |');
print('|-------------------------|');
print('| Test Game |');
print('|=========================|');
print('');
stdout.write('Press enter to continue: ');
// detect key press
stdin.readLineSync();
clearTerminal();
gameHandler();
}