-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticTacToe-checkpoint
197 lines (197 loc) · 6.65 KB
/
ticTacToe-checkpoint
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import clear_output\n",
"import random\n",
"\n",
"def displayBoard(board):\n",
" '''\n",
" Create player display board\n",
" '''\n",
" clear_output()\n",
" print(board[7]+'|'+ board[8] +'|'+ board[9])\n",
" print(\"------\")\n",
" print(board[4]+'|'+ board[5] +'|'+ board[6])\n",
" print(\"------\")\n",
" print(board[1]+'|'+ board[2] +'|'+ board[3])\n",
"\n",
"def playerInput():\n",
" marker = \"\"\n",
" while not (marker == 'X' or marker == \"O\"):\n",
" marker = input('Player1: Choose X or O').upper()\n",
" if marker == 'X':\n",
" return (\"X\", \"O\")\n",
" else:\n",
" return (\"O\", \"X\")\n",
"\n",
"def placeMarker(board, marker, position):\n",
" board[position] = marker\n",
"\n",
"def checkWinner(board, mark):\n",
" # Tic tac Toe winner conditions\n",
" #Check all the rows(all three) if they are at same level(X/O)\n",
" if ((board[1] == mark and board[2] == mark and board[3] == mark) or \n",
" (board[4] == mark and board[5] == mark and board[6] == mark) or\n",
" (board[7] == mark and board[8] == mark and board[9] == mark)):\n",
" return True\n",
" # else:\n",
" # return False\n",
" \n",
" #Check for all three columns \n",
" if ((board[1] == mark and board[4] == mark and board[7] == mark) or \n",
" (board[2] == mark and board[5] == mark and board[8] == mark) or\n",
" (board[3] == mark and board[6] == mark and board[9] == mark)):\n",
" return True\n",
"# else:\n",
"# return False\n",
" \n",
" #Check for all two diagonals \n",
" if ((board[1] == mark and board[5] == mark and board[9] == mark) or \n",
" (board[3] == mark and board[5] == mark and board[7] == mark)):\n",
" return True\n",
" else:\n",
" return False\n",
" #We can check all above conditions in single return block but this look more readAble TO ME\n",
"\n",
"def flipCoin():\n",
" flip = random.randint(0, 1)\n",
" if flip == 1:\n",
" return \"Player 1\"\n",
" else:\n",
" return \"Player 2\"\n",
" \n",
"def selectedBoardPositionFree(board, position):\n",
" return board[position] == \" \"\n",
"\n",
"def checkFullBoard(board):\n",
" for i in range(1, 10):\n",
" if selectedBoardPositionFree(board, i):\n",
" return False\n",
" return True\n",
"\n",
"def playersChoice(board):\n",
" position = 0\n",
" while position not in [1,2,3,4,5,6,7,8,9] or not selectedBoardPositionFree(board, position):\n",
" position = int(input(\"Choose a position out of 1-9\"))\n",
" return position\n",
"\n",
"def replay():\n",
" choice = input(\"play again ? (yes/no)\").upper()\n",
" return choice == \"YES\"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X| | \n",
"------\n",
"O|X|O\n",
"------\n",
"X|O|X\n",
"Player 2 is won\n",
"play again ? (yes/no)8\n"
]
}
],
"source": [
"#Welcome to Tic Tac Toe - Lets Playe the game \n",
"print('Welcome to Tic Tac Toe - Lets Playe the game ')\n",
"while True:\n",
" #start the game and setup\n",
" \n",
" #setup the board, first index will be left as game meant for lay man users\n",
" board = [' ']*10\n",
" \n",
" player1_marker, player2_marker = playerInput()\n",
" turn = flipCoin()\n",
" print(\"Toss Result: \" + turn + ' will play first')\n",
" play_game = input(\"Ready to play game ? y or n\").upper()\n",
" if play_game == 'Y':\n",
" game_on = True\n",
" else:\n",
" game_on = False\n",
" while game_on:\n",
" if turn == \"Player 1\":\n",
" #display the current board for decition making \n",
" displayBoard(board)\n",
" #let player 1 choose the position\n",
" position = playersChoice(board)\n",
" #once the decision is made, place the marker at selected position\n",
" placeMarker(board, player1_marker, position)\n",
" #now we must check for winner or tie\n",
" if checkWinner(board, player1_marker):\n",
" displayBoard(board)\n",
" print('Player 1 is won')\n",
" game_on = False\n",
" else:\n",
" if checkFullBoard(board):\n",
" displayBoard(board)\n",
" print(\"Match Tie\")\n",
" game_on = False\n",
" else:\n",
" turn = 'Player 2'\n",
" else:\n",
" #display the current board for decition making \n",
" displayBoard(board)\n",
" #let player 2 choose the position\n",
" position = playersChoice(board)\n",
" #once the decision is made, place the marker at selected position\n",
" placeMarker(board, player2_marker, position)\n",
" #now we must check for winner or tie\n",
" if checkWinner(board, player2_marker):\n",
" displayBoard(board)\n",
" print('Player 2 is won')\n",
" game_on = False\n",
" else:\n",
" if checkFullBoard(board):\n",
" displayBoard(board)\n",
" print(\"Match Tie\")\n",
" game_on = False\n",
" else:\n",
" turn = 'Player 1'\n",
" #if no one interested in playing again\n",
" if not replay():\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}