-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (141 loc) · 5.82 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#main code for the game!
import numpy as np
import os
from functions import *
from board import Board
from constants import *
def main():
welcome()
name = input("Set the name for the player: ").strip()
print(f"Welcome {name} to this Battleship game!")
while True:
rule = input("Do you want me to remind you the rules? ('yes' or 'no'): ").strip()
if rule.lower() =='yes':
playing = show_rules()
break
elif rule.lower() =='no':
playing = True
break
else:
print("Please reply either 'yes' or 'no'")
os.system('cls' if os.name == 'nt' else "printf '\033c'") #this should work on terminal to flush output!
if playing:
print("Good! Let's start with the game!")
else:
print("Ok, so maybe next time!")
return None
my_board = Board(name, BOARD_SIZE, BOATS)
cpu_board = Board("Computer",BOARD_SIZE, BOATS)
print("""
This is your board with boats set randomly
Notation: | * | is water
< B > is a boat piece
( X ) is a spot hit by the enemy
""")
print(show_board(my_board))
#flip a coin to decide who starts
coin_dict = {0 : "head", 1 : "tail"}
player_starts = False
while True:
coin = input("Choose head or tail: ").strip()
if coin.lower() not in ["head", "tail"]:
print(f"You must write either 'head' or 'tail' but you wrote {coin}")
else:
break
if coin_dict[np.random.randint(0,2)]==coin.lower():
player_starts = True
os.system('cls' if os.name == 'nt' else "printf '\033c'") #this should work on terminal to flush output!
#player starts
while True:
if player_starts:
is_hit = True
#player turn
print(f"*********** {my_board.player}'s turn starts now'! ***********")
while is_hit:
print(show_hits(my_board, cpu_board))
is_hit = player_turn(my_board, cpu_board)
#os.system('cls' if os.name == 'nt' else "printf '\033c'") #this should work on terminal to flush output!
victory_player = check_victory(cpu_board)
if victory_player:
break
if victory_player:
pattern = """\/"""
string = f"---- Congratulations {my_board.player}, you won!!!! ----"
print(show_hits(my_board, cpu_board))
print(" You sank all the boats!")
print("The Computer only sank these boats:")
print(f"{my_board.get_sunk_boats()}\n")
print(f"""
{pattern*int(len(string)/2)}
{string}
{pattern*int(len(string)/2)}
""")
break
is_hit = True
#cpu turn
print("*********** The turn of the Computer starts now! ***********")
while is_hit:
is_hit = cpu_turn(cpu_board, my_board)
victory_cpu = check_victory(my_board)
if victory_cpu:
break
if victory_cpu:
pattern = """\/"""
string = f"---- Game Over! The Computer won!!!! ----"
print(show_hits(my_board, cpu_board))
print(" The Computer sank all the boats!")
print("You only sank these boats:")
print(f"{my_board.get_sunk_boats()}\n")
print(f"""
{pattern*int(len(string)/2)}
{string}
{pattern*int(len(string)/2)}
""")
break
else:
is_hit = True
#cpu turn
print("*********** The turn of the Computer starts now! ***********")
while is_hit:
is_hit = cpu_turn(cpu_board, my_board)
victory_cpu = check_victory(my_board)
if victory_cpu:
break
if victory_cpu:
pattern = """\/"""
string = f"---- Game Over! The Computer won!!!! ----"
print(show_hits(my_board, cpu_board))
print(show_hits(my_board, cpu_board))
print(" The Computer sank all the boats!")
print("You only sank these boats:")
print(f"{my_board.get_sunk_boats()}\n")
print(f"""
{pattern*int(len(string)/2)}
{string}
{pattern*int(len(string)/2)}
""")
break
is_hit = True
#player turn
print(f"*********** {my_board.player}'s turn starts now! ***********")
while is_hit:
print(show_hits(my_board, cpu_board))
is_hit = player_turn(my_board, cpu_board)
#os.system('cls' if os.name == 'nt' else "printf '\033c'") #this should work on terminal to flush output!
victory_player = check_victory(cpu_board)
if victory_player:
break
if victory_player:
pattern = """\/"""
string = f"---- Congratulations {my_board.player}, you won!!!! ----"
print(show_hits(my_board, cpu_board))
print(" You sank all the boats!")
print("The Computer only sank these boats:")
print(f"{my_board.get_sunk_boats()}\n")
print(f"""
{pattern*int(len(string)/2)}
{string}
{pattern*int(len(string)/2)}
""")
break
main()