@@ -20,18 +20,20 @@ def welcome():
2020 print ("Rules: " )
2121 print ("1. Enter coordinates to put the pieces on the board." )
2222 print ("2. Enter (0, 0) to skip the round." )
23- print ("3. Try to fill the board in as few rounds as possible!\n " )
23+ print ("3. Try to fill the board in as few rounds as possible!" )
24+ print ("4. Press Ctrl+C to interrupt and close the game.\n " )
2425
2526 print ("Please enter the board size you want." )
26- height = 3
27- width = 3
27+
2828 try :
2929 height = int (input ("Height (default: 3) -> " ))
3030 width = int (input ("Width (default: 3) -> " ))
31- except :
31+ except ValueError :
3232 print ("Invalid height. Using a 3x3 board as default." )
3333 height = 3
3434 width = 3
35+ except KeyboardInterrupt :
36+ raise
3537
3638 return height , width
3739
@@ -44,11 +46,21 @@ def is_game_done(game_board):
4446
4547 return True
4648
47- def print_board (game_board ):
48- for row in game_board :
49+ def print_board (game_board , board_width ):
50+ """
51+ Print the game board with column numbers and row numbers.
52+ """
53+ column_header = [" " ]
54+ for column in range (1 , board_width + 1 ):
55+ column_header .append (str (column ))
56+ print (" " .join (column_header ))
57+
58+ for row_index , row in enumerate (game_board , start = 1 ):
59+ row_parts = [f"{ row_index :2} " ]
4960 for cell in row :
50- print (cell , end = " " )
51- print ()
61+ row_parts .append (cell )
62+ print (" " .join (row_parts ))
63+
5264
5365# Prints the piece onto the game screen
5466def print_piece (current_piece ):
@@ -78,8 +90,10 @@ def get_player_input():
7890 try :
7991 x = int (input ("Column -> " ))
8092 y = int (input ("Row -> " ))
81- except :
93+ except ValueError :
8294 print ("Invalid coordinates. Please re-enter." )
95+ except KeyboardInterrupt :
96+ raise
8397
8498 return x , y
8599
@@ -129,42 +143,46 @@ def update_board(game_board, height, width, x, y, current_piece):
129143 return new_board
130144
131145if __name__ == "__main__" :
146+ try :
147+ # Welcome the player
148+ clear ()
149+ height , width = welcome ()
150+ print ()
132151
133- # Welcome the player
134- clear ()
135- height , width = welcome ()
136- print ()
152+ # Create a heightxwidth board
153+ game_board = [(["□" ] * width ) for x in range (height )]
154+
155+ # Attributes of the game
156+ game_round = 0
157+ current_piece = 0
137158
138- # Create a heightxwidth board
139- game_board = [(["□" ] * width ) for x in range (height )]
159+ # Main game loop
140160
141- # Attributes of the game
142- game_round = 0
143- current_piece = 0
161+ while not is_game_done (game_board ):
144162
145- # Main game loop
146- while not is_game_done (game_board ):
163+ # Start new round
164+ game_round += 1
165+ print (f"______________Round: { game_round } ______________" )
147166
148- # Start new round
149- game_round += 1
150- print (f"______________Round: { game_round } ______________" )
167+ # Display current piece
168+ print ("Current piece:" )
169+ current_piece = randint (0 ,4 )
170+ print_piece (current_piece )
151171
152- # Display current piece
153- print ("Current piece:" )
154- current_piece = randint (0 ,4 )
155- print_piece (current_piece )
172+ # Display game board
173+ print ("Game board:" )
174+ print_board (game_board , width )
156175
157- # Display game board
158- print ( "Game board:" )
159- print_board (game_board )
176+ # Get player input
177+ x , y = get_player_input ( )
178+ game_board = update_board (game_board , height , width , x , y , current_piece )
160179
161- # Get player input
162- x , y = get_player_input ()
163- game_board = update_board (game_board , height , width , x , y , current_piece )
180+ print (f"\n ______________Round: { game_round } ______________\n " )
164181
165- print (f"\n ______________Round: { game_round } ______________\n " )
182+ # Game over
183+ print ("Game over!" )
184+ print_board (game_board , width )
185+ print (f"\n You completed the board in { game_round } rounds!" )
166186
167- # Game over
168- print ("Game over!" )
169- print_board (game_board )
170- print (f"\n You completed the board in { game_round } rounds!" )
187+ except KeyboardInterrupt :
188+ print ("\n \n Exiting Game.\n " )
0 commit comments