forked from alonm16/battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
64 lines (56 loc) · 2.13 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
from Human import Human
from Computer import Computer
import socket
FORMAT = 'utf-8'
class Game:
def __init__(self):
self.player0 = Human()
self.player1 = None
def single_player(self):
"""
single player game against the computer, with option for easy/hard mode
:return:
"""
mode = input('Choose game mode (easy/hard): ').lower()
while mode not in ['easy', 'hard']:
mode = input('please enter a valid input: ')
self.player1 = Computer(mode)
self.player0.place_battleships()
self.player1.place_battleships()
while True:
result = self.player0.turn(self.player1)
if result == 'win':
print('player 0 has won!')
return
result = self.player1.turn(self.player0)
if result == 'win':
print('player 1 has won!')
return
def multi_player(self):
"""
multiplayer game, the second player can join through running SecondPlayer.py
:return:
"""
self.player0.output('waiting for another player...')
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((socket.gethostname(), 1234))
server.listen(1)
conn, addr = server.accept()
self.player1 = Human(conn)
self.player1.output('waiting for opponent to place battleships...')
self.player0.place_battleships()
self.player0.output('waiting for opponent to place battleships...')
self.player1.place_battleships()
while True:
self.player1.output('Opponent\'s turn:')
result = self.player0.turn(self.player1)
if result == 'win':
self.player0.output('player 0 has won!')
self.player1.output('player 0 has won!')
return
self.player0.output('Opponent\'s turn:')
result = self.player1.turn(self.player0)
if result == 'win':
self.player0.output('player 1 has won!')
self.player1.output('player 1 has won!')
return