forked from fubrasp/puissance4Like
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
68 lines (56 loc) · 2.18 KB
/
Game.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import time
import random
from GameView import *
class Game:
NUMBER_OF_CHIPS = 42
def __init__(self):
self.gamer = 1
self.playedChips = 0
self.potentialWinner = False
self.gameView = GameView()
def get_gamer(self):
# Cette fonction retourne le numero du joueur qui doit jouer
if self.playedChips % 2 == 0:
gamer_id = GameBoard.YELLOW_CHIP
else:
gamer_id = GameBoard.RED_CHIP
return gamer_id
def display_winner(self):
if self.gamer == "" or self.gamer is None:
return "personne n'a gagne"
else:
return self.gamer + " a gagne"
def start(self):
while self.potentialWinner != "jaune" \
and self.potentialWinner != "rouge" \
and self.playedChips < Game.NUMBER_OF_CHIPS:
time.sleep(0.05)
# Le joueur joue
gamer = self.get_gamer()
if gamer == GameBoard.RED_CHIP:
self.random_move(gamer)
else:
for event in self.gameView.pyGame.event.get():
self.gameView.gameBoard.display()
if event.type == self.gameView.pyGame.MOUSEBUTTONUP:
x, y = self.gameView.pyGame.mouse.get_pos()
gamer = self.get_gamer()
column = self.gameView.determine_column(x)
self.make_move(column, gamer)
if event.type == self.gameView.pyGame.QUIT:
sys.exit(0)
def random_move(self, gamer):
available_columns = [i for i in range(7) if self.gameView.gameBoard.is_column_available(i)]
if available_columns:
column = random.choice(available_columns)
self.make_move(column, gamer)
def make_move(self, column, gamer):
self.gameView.gameBoard.put_chip(column, gamer)
self.playedChips += 1
self.potentialWinner = self.gameView.gameBoard.get_winner()
print("Gagnant ? : " + str(self.potentialWinner))
self.gameView.render()
self.gameView.pyGame.display.flip()