forked from KUeecs448-group5/eecs448-project1
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
142 lines (128 loc) · 4.56 KB
/
main.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
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
#Name: Caden Kroonenberg
import Print, shotDetection, shipPlacement2, aiPlacement, aiShotDetection, scoreBoardLogic
from termcolor import colored
def run(shipCount):
"""
implements every method of battleship game and checks for winner every turn
shipCount: number of ships(potentially of various sizes) that each player has.
Precondition: player variable initialized.
Postcondition: checks win condition every turn and declares winner when one player has no ships remaining.
"""
endGame = False
player = 1
print()
print("Player 1:")
print()
shipPlacement2.placeShip(0, shipCount)
player = 2
input("Press Enter and then switch players to continue...")
print(chr(27) + "[2J")
print("Player 2:")
print()
shipPlacement2.placeShip(1, shipCount)
player = 1
input("Press Enter and then switch players to continue...")
print(chr(27) + "[2J")
scoreBoardLogic.initializeScore(shipPlacement2.playArr[0], shipPlacement2.playArr[1])
while not endGame:
if player == 1:
print("Player 1:")
elif player == 2:
print("Player 2:")
Print.printTopMap(player)
print()
Print.printBottomMap(player)
shotDetection.shot(player)
#check win condition and switch players if not
if player == 1:
if shotDetection.p1shotCount >= winCount:
endGame = True
print("\nPlayer 1 Wins!\n")
else:
player = 2
print(chr(27) + "[2J")
elif player == 2:
if shotDetection.p2shotCount == winCount:
endGame = True
print("\nPlayer 2 Wins!\n")
else:
player = 1
print(chr(27) + "[2J")
def runAI(shipCount):
"""
implements every method of battleship game and checks for winner every turn
shipCount: number of ships(potentially of various sizes) that each player has.
Precondition: player variable initialized.
Postcondition: checks win condition every turn and declares winner when one player has no ships remaining.
"""
endGame = False
player = 1
print()
print("Player 1:")
print()
shipPlacement2.placeShip(0, shipCount)
player = 2
input("Press Enter and then switch players to continue...")
print(chr(27) + "[2J")
print("Player 2:")
print()
aiPlacement.placeShip(1, shipCount)
player = 1
input("Press Enter and then switch players to continue...")
print(chr(27) + "[2J")
scoreBoardLogic.initializeScore(shipPlacement2.playArr[0], shipPlacement2.playArr[1])
while not endGame:
if player == 1:
print("Player 1:")
elif player == 2:
print("Player 2:")
Print.printTopMap(player)
print()
Print.printBottomMap(player)
# shotDetection.shot(player) original
aiShotDetection.shot(player)
#check win condition and switch players if not
if player == 1:
if aiShotDetection.p1shotCount >= winCount: # original shotDetection.p1shotCount >= winCount:
endGame = True
print("\nPlayer 1 Wins!\n")
else:
player = 2
print(chr(27) + "[2J")
elif player == 2:
if aiShotDetection.p2shotCount == winCount: # original shotDetection.p2shotCount == winCount:
endGame = True
print("\nPlayer 2 Wins!\n")
else:
player = 1
print(chr(27) + "[2J")
print(chr(27) + "[2J")
print(colored("BATTLESHIP", 'blue', 'on_grey'))
print()
repeat = True
while repeat == True:
repeat = False
shipCount = input("Enter ship count [1-6]: ")
if not shipCount.isnumeric():
print("Invalid input (not an integer). Try again")
repeat = True
elif int(shipCount) > 6 or int(shipCount) < 1:
print("Invalid input. Try again")
repeat = True
winCount = 0
for i in range(1,int(shipCount)+1):
winCount = winCount + i
userInput = input("If you would like to play against the computer type 'computer', otherwise type any characters for a two-player game ")
if (userInput == "computer"):
level = input("Please choose a difficulty level: Type 'Easy', 'Medium', or 'Hard' ")
if (level == "Medium"):
aiShotDetection.ai = 1
elif (level == "Hard"):
aiShotDetection.ai = 2
# default is easy level
else:
aiShotDetection.ai = 0
if userInput == "computer":
runAI(shipCount)
else:
run(shipCount)