Skip to content

Commit caa5a79

Browse files
committed
file handling binary format
1 parent 8503e07 commit caa5a79

7 files changed

+1069
-316
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

Untitled3.ipynb

+318
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "0a9f0951",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"Do you want to play again? (y/n)y\n",
14+
"-------------------\n",
15+
"Welcome to the game!\n",
16+
" | | \n",
17+
" | | \n",
18+
" | | \n",
19+
"------------\n",
20+
" | | \n",
21+
" | | \n",
22+
" | | \n",
23+
"------------\n",
24+
" | | \n",
25+
" | | \n",
26+
" | | \n",
27+
"please select a position to enter the X between 1 to 9: 3\n",
28+
" | | \n",
29+
" | | X\n",
30+
" | | \n",
31+
"------------\n",
32+
" | | \n",
33+
" | | \n",
34+
" | | \n",
35+
"------------\n",
36+
" | | \n",
37+
" | | \n",
38+
" | | \n",
39+
"computer placed an o on position 9 :\n",
40+
" | | \n",
41+
" | | X\n",
42+
" | | \n",
43+
"------------\n",
44+
" | | \n",
45+
" | | \n",
46+
" | | \n",
47+
"------------\n",
48+
" | | \n",
49+
" | | O\n",
50+
" | | \n",
51+
"please select a position to enter the X between 1 to 9: 5\n",
52+
" | | \n",
53+
" | | X\n",
54+
" | | \n",
55+
"------------\n",
56+
" | | \n",
57+
" | X | \n",
58+
" | | \n",
59+
"------------\n",
60+
" | | \n",
61+
" | | O\n",
62+
" | | \n",
63+
"computer placed an o on position 7 :\n",
64+
" | | \n",
65+
" | | X\n",
66+
" | | \n",
67+
"------------\n",
68+
" | | \n",
69+
" | X | \n",
70+
" | | \n",
71+
"------------\n",
72+
" | | \n",
73+
" O | | O\n",
74+
" | | \n",
75+
"please select a position to enter the X between 1 to 9: 8\n",
76+
" | | \n",
77+
" | | X\n",
78+
" | | \n",
79+
"------------\n",
80+
" | | \n",
81+
" | X | \n",
82+
" | | \n",
83+
"------------\n",
84+
" | | \n",
85+
" O | X | O\n",
86+
" | | \n",
87+
"computer placed an o on position 2 :\n",
88+
" | | \n",
89+
" | O | X\n",
90+
" | | \n",
91+
"------------\n",
92+
" | | \n",
93+
" | X | \n",
94+
" | | \n",
95+
"------------\n",
96+
" | | \n",
97+
" O | X | O\n",
98+
" | | \n",
99+
"please select a position to enter the X between 1 to 9: 4\n",
100+
" | | \n",
101+
" | O | X\n",
102+
" | | \n",
103+
"------------\n",
104+
" | | \n",
105+
" X | X | \n",
106+
" | | \n",
107+
"------------\n",
108+
" | | \n",
109+
" O | X | O\n",
110+
" | | \n",
111+
"computer placed an o on position 6 :\n",
112+
" | | \n",
113+
" | O | X\n",
114+
" | | \n",
115+
"------------\n",
116+
" | | \n",
117+
" X | X | O\n",
118+
" | | \n",
119+
"------------\n",
120+
" | | \n",
121+
" O | X | O\n",
122+
" | | \n",
123+
"please select a position to enter the X between 1 to 9: 1\n",
124+
" | | \n",
125+
" X | O | X\n",
126+
" | | \n",
127+
"------------\n",
128+
" | | \n",
129+
" X | X | O\n",
130+
" | | \n",
131+
"------------\n",
132+
" | | \n",
133+
" O | X | O\n",
134+
" | | \n"
135+
]
136+
},
137+
{
138+
"ename": "TypeError",
139+
"evalue": "list indices must be integers or slices, not NoneType",
140+
"output_type": "error",
141+
"traceback": [
142+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
143+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
144+
"Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 128>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 130\u001b[0m board \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m10\u001b[39m)]\n\u001b[0;32m 131\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-------------------\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m--> 132\u001b[0m \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 133\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 134\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n",
145+
"Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 113\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 114\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 115\u001b[0m \u001b[43minsertLetter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mO\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmove\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 116\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcomputer placed an o on position\u001b[39m\u001b[38;5;124m'\u001b[39m, move, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 117\u001b[0m printBoard(board)\n",
146+
"Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36minsertLetter\u001b[1;34m(letter, pos)\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21minsertLetter\u001b[39m(letter,pos):\n\u001b[1;32m----> 4\u001b[0m board[pos] \u001b[38;5;241m=\u001b[39m letter\n",
147+
"\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not NoneType"
148+
]
149+
}
150+
],
151+
"source": [
152+
"board = [' ' for x in range(10)]\n",
153+
"\n",
154+
"def insertLetter(letter,pos):\n",
155+
" board[pos] = letter\n",
156+
"\n",
157+
"def spaceIsFree(pos):\n",
158+
" return board[pos] == ' '\n",
159+
"\n",
160+
"def printBoard(board):\n",
161+
" print(' | | ')\n",
162+
" print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])\n",
163+
" print(' | | ')\n",
164+
" print('------------')\n",
165+
" print(' | | ')\n",
166+
" print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])\n",
167+
" print(' | | ')\n",
168+
" print('------------')\n",
169+
" print(' | | ')\n",
170+
" print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])\n",
171+
" print(' | | ')\n",
172+
"\n",
173+
"def isBoardFull(board):\n",
174+
" if board.count(' ') > 1:\n",
175+
" return False\n",
176+
" else:\n",
177+
" return True\n",
178+
"\n",
179+
"def IsWinner(b,l):\n",
180+
" return(\n",
181+
" (b[1] == l and b[2] == l and b[3] == l) or\n",
182+
" (b[4] == l and b[5] == l and b[6] == l) or\n",
183+
" (b[7] == l and b[8] == l and b[9] == l) or\n",
184+
" (b[1] == l and b[4] == l and b[7] == l) or\n",
185+
" (b[2] == l and b[5] == l and b[8] == l) or\n",
186+
" (b[3] == l and b[6] == l and b[9] == l) or\n",
187+
" (b[1] == l and b[5] == l and b[9] == l) or\n",
188+
" (b[3] == l and b[5] == l and b[7] == l)\n",
189+
" )\n",
190+
"\n",
191+
"def playerMove():\n",
192+
" run = True\n",
193+
" while run:\n",
194+
" move = input(\"please select a position to enter the X between 1 to 9: \")\n",
195+
" try:\n",
196+
" move = int(move)\n",
197+
" if move > 0 and move < 10:\n",
198+
" if spaceIsFree(move):\n",
199+
" run = False\n",
200+
" insertLetter('X', move)\n",
201+
" else:\n",
202+
" print('Sorry, this space is occupied')\n",
203+
" else:\n",
204+
" print('please type a number between 1 and 9')\n",
205+
" \n",
206+
" except:\n",
207+
" print('Please type a number')\n",
208+
"\n",
209+
"def computerMove():\n",
210+
" possibleMoves = [ x for x, letter in enumerate(board) if letter == ' ' and x != 0]\n",
211+
" move = 0\n",
212+
"\n",
213+
" for let in ['O', 'X']:\n",
214+
" for i in possibleMoves:\n",
215+
" boardcopy = board[:]\n",
216+
" boardcopy[i] = let\n",
217+
" if IsWinner(boardcopy, let):\n",
218+
" move = i\n",
219+
" return move\n",
220+
"\n",
221+
" cornersOpen = []\n",
222+
" for i in possibleMoves:\n",
223+
" if i in [1, 3, 7, 9]:\n",
224+
" cornersOpen.append(i)\n",
225+
"\n",
226+
" if len(cornersOpen) > 0:\n",
227+
" move = selectRandom(cornersOpen)\n",
228+
" return move\n",
229+
"\n",
230+
" if 5 in possibleMoves:\n",
231+
" move = 5\n",
232+
" return move\n",
233+
"\n",
234+
" edgesOpen = []\n",
235+
" for i in possibleMoves:\n",
236+
" if i in [2, 4, 6, 8]:\n",
237+
" edgesOpen.append(i)\n",
238+
"\n",
239+
" if len(edgesOpen) > 0:\n",
240+
" move = selectRandom(edgesOpen)\n",
241+
" return move\n",
242+
"\n",
243+
"def selectRandom(li):\n",
244+
" import random\n",
245+
" ln = len(li)\n",
246+
" r = random.randrange(0, ln)\n",
247+
" return li[r]\n",
248+
"\n",
249+
"def main():\n",
250+
" print(\"Welcome to the game!\")\n",
251+
" printBoard(board)\n",
252+
"\n",
253+
" while not(isBoardFull(board)):\n",
254+
" if not(IsWinner(board, 'O')):\n",
255+
" playerMove()\n",
256+
" printBoard(board)\n",
257+
" else:\n",
258+
" print(\"sorry you loose!\")\n",
259+
" break\n",
260+
"\n",
261+
" if not(IsWinner(board, 'X')):\n",
262+
" move = computerMove()\n",
263+
" if move == 0:\n",
264+
" print(\" \")\n",
265+
" else:\n",
266+
" insertLetter('O', move)\n",
267+
" print('computer placed an o on position', move, ':')\n",
268+
" printBoard(board)\n",
269+
" else:\n",
270+
" print(\"you win!\")\n",
271+
" break\n",
272+
"\n",
273+
"\n",
274+
"\n",
275+
" if isBoardFull(board):\n",
276+
" print(\"Tie game\")\n",
277+
"\n",
278+
"while True:\n",
279+
" x = input(\"Do you want to play again? (y/n)\")\n",
280+
" if x.lower() == 'y':\n",
281+
" board = [' ' for x in range(10)]\n",
282+
" print('-------------------')\n",
283+
" main()\n",
284+
" else:\n",
285+
" break"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": null,
291+
"id": "cf1d0d7c",
292+
"metadata": {},
293+
"outputs": [],
294+
"source": []
295+
}
296+
],
297+
"metadata": {
298+
"kernelspec": {
299+
"display_name": "Python 3 (ipykernel)",
300+
"language": "python",
301+
"name": "python3"
302+
},
303+
"language_info": {
304+
"codemirror_mode": {
305+
"name": "ipython",
306+
"version": 3
307+
},
308+
"file_extension": ".py",
309+
"mimetype": "text/x-python",
310+
"name": "python",
311+
"nbconvert_exporter": "python",
312+
"pygments_lexer": "ipython3",
313+
"version": "3.9.12"
314+
}
315+
},
316+
"nbformat": 4,
317+
"nbformat_minor": 5
318+
}

binary_file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello this is

0 commit comments

Comments
 (0)