Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6b10f8

Browse files
Update tictactoe.py
1 parent c121569 commit e6b10f8

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

‎TicTacToe/tictactoe.py‎

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,71 @@
1-
'''This is a simple game called TicTacToe and must be played with 2 players'''
1+
"""
2+
This is a simple game called TicTacToe and must be played with 2 players
3+
The IDE used is PyCharm
4+
"""
25
import random
3-
from IPython.display import clear_output
46

57
board = ['k', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
68
available = [str(num) for num in range(0, 10)]
9+
10+
711
def display_board(a, b):
8-
'''Display the board game the the available spots'''
9-
print('Available GAME BOARD\n'+
10-
' Moves \n\n '+
11-
a[7] + '|' + a[8] + '|' + a[9]+' '+b[7] + '|' + b[8] + '|' + b[9]+'\n '+
12-
a[4] + '|' + a[5] + '|' + a[6]+' '+b[4] + '|' + b[5] + '|' + b[6]+'\n '+
13-
a[1] + '|' + a[2] + '|' + a[3]+' '+b[1] + '|' + b[2] + '|' + b[3])
12+
"""Display the board game the the available spots"""
13+
print('Available GAME BOARD\n' +
14+
' Moves \n\n ' +
15+
a[7] + '|' + a[8] + '|' + a[9] + ' ' + b[7] + '|' + b[8] + '|' + b[9] + '\n ' +
16+
a[4] + '|' + a[5] + '|' + a[6] + ' ' + b[4] + '|' + b[5] + '|' + b[6] + '\n ' +
17+
a[1] + '|' + a[2] + '|' + a[3] + ' ' + b[1] + '|' + b[2] + '|' + b[3])
18+
1419

1520
def welcome():
16-
'''A welcome message'''
21+
"""A welcome message"""
1722
print('Welcome to Tic-Tac_toe. It is a game for two players')
1823
print('The first to go will be chosen randomly')
1924

25+
2026
def ask_marker():
2127
'''Ask for a marker from the preference of the player'''
2228
marker = input("Player 1, which marker do you wanna be? X or O? ")
2329
while (marker.upper() != 'X') and (marker.upper() != 'O'):
2430
marker = input("Just X or O pls: ")
2531
return marker.upper()
2632

33+
2734
def check_win(board, marker):
28-
'''Return true if the player wins'''
29-
return((board[1] == board[2] == board[3] == marker) or #first horizontal
30-
(board[4] == board[5] == board[6] == marker) or #second horizontal
31-
(board[7] == board[8] == board[9] == marker) or #third horizontal
32-
(board[1] == board[4] == board[7] == marker) or #first vertical
33-
(board[2] == board[5] == board[8] == marker) or #second vertical
34-
(board[3] == board[6] == board[9] == marker) or #third vertical
35-
(board[1] == board[5] == board[9] == marker) or #first diagonal
36-
(board[3] == board[5] == board[7] == marker)) #second diagonal
35+
"""Return true if the player wins"""
36+
return ((board[1] == board[2] == board[3] == marker) or # first horizontal
37+
(board[4] == board[5] == board[6] == marker) or # second horizontal
38+
(board[7] == board[8] == board[9] == marker) or # third horizontal
39+
(board[1] == board[4] == board[7] == marker) or # first vertical
40+
(board[2] == board[5] == board[8] == marker) or # second vertical
41+
(board[3] == board[6] == board[9] == marker) or # third vertical
42+
(board[1] == board[5] == board[9] == marker) or # first diagonal
43+
(board[3] == board[5] == board[7] == marker)) # second diagonal
44+
3745

3846
def check_full(board):
39-
'''Return true if the board is full'''
47+
"""Return true if the board is full"""
4048
hold = 1
4149
for x in board:
4250
if x == ' ':
4351
hold += 1
4452
return hold == 1
4553

54+
4655
def place_marker(board, available, marker, position):
47-
'''Place the marker in the position into the board and clear the same position from availables'''
56+
"""Place the marker in the position into the board and clear the same position from availables"""
4857
board[position] = marker
4958
available[position] = ' '
5059

60+
5161
def check_whitespace(board, position):
52-
'''Return True if there is a white space in the position of the board'''
62+
"""Return True if there is a white space in the position of the board"""
5363
return board[position] == ' '
5464

65+
5566
def ask_input(board, name, marker):
56-
'''Ask the player where he wants to place the marker in the board'''
57-
print('{}, type the positon(1-9) for you marker({}): '.format(name, marker))
67+
"""Ask the player where he wants to place the marker in the board"""
68+
print('{}, type the position(1-9) for you marker({}): '.format(name, marker))
5869
position = 0
5970
while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not check_whitespace(board, position):
6071
try:
@@ -67,16 +78,18 @@ def ask_input(board, name, marker):
6778
print("Invalid position, try again pls")
6879
return position
6980

81+
7082
def get_random():
71-
'''Chose randomly from Player 1 and 2'''
83+
"""Chose randomly from Player 1 and 2"""
7284
get_first = random.randint(1, 2)
7385
if get_first == 1:
7486
return 'Player 1'
7587
else:
7688
return 'Player 2'
7789

90+
7891
def play_again():
79-
'''Return true if want to play again.'''
92+
"""Return true if want to play again."""
8093
option = " "
8194
option = str(input("Do you want to play again?? (Yes or No): "))
8295
while (option[0].upper() != 'Y') and (option[0].upper() != 'N'):
@@ -101,7 +114,7 @@ def play_again():
101114
if next_player == 'Player 1':
102115
display_board(available, board)
103116
place_marker(board, available, marker_p1, ask_input(board, next_player, marker_p1))
104-
clear_output()
117+
print('\n'*20) # prints 20 lines to separate the outputs.
105118
if check_win(board, marker_p1):
106119
print("The {} ({}) won. Congrats! ".format(next_player, marker_p1))
107120
display_board(available, board)
@@ -115,7 +128,7 @@ def play_again():
115128
elif next_player == 'Player 2':
116129
display_board(available, board)
117130
place_marker(board, available, marker_p2, ask_input(board, next_player, marker_p2))
118-
clear_output()
131+
print('\n'*20) # prints 20 lines to separate the outputs.
119132
if check_win(board, marker_p2):
120133
print("The {} ({}) won. Congrats! ".format(next_player, marker_p2))
121134
display_board(available, board)
@@ -127,10 +140,10 @@ def play_again():
127140
next_player = 'Player 1'
128141

129142
if not play_again():
130-
clear_output()
143+
print('\n'*20) # prints 20 lines to separate the outputs.
131144
print('Thank you for playing :)')
132145
break
133146
else:
134147
board = ['k', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
135148
available = [str(num) for num in range(0, 10)]
136-
clear_output()
149+
print('\n'*20) # prints 20 lines to separate the outputs.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /