Tic tac toe game

Milestone 1 basic code for Udemy's Bootcamp Python Course

The project is to make a Tic tac toe game.Print the game board after every move!
Try it yourself it's the easiest thing I have ever done!I did it in 30 mins. So it can take time depending
on your Python and Understanding of the game itself.

This is how it looks like


So here's the code in Python.
Please follow me on github: www.github.com/fivecube

1:  def check(board,k):  
2:    if board[0][0]==k and board[1][1]==k and board[2][2]==k:  
3:      print(k,"wins")  
4:      return 1  
5:    if board[0][0] == k and board[0][1]==k and board[0][2]==k:  
6:      print(k, "wins")  
7:      return 1  
8:    if board[0][0] == k and board[1][0]==k and board[2][0]==k:  
9:      print(k, "wins")  
10:      return 1  
11:    if board[1][0] == k and board[1][1]==k and board[1][2]==k:  
12:      print(k, "wins")  
13:      return 1  
14:    if board[2][0] == k and board[2][1]==k and board[2][2]==k:  
15:      print(k, "wins")  
16:      return 1  
17:    if board[0][2] == k and board[1][1]==k and board[2][0]==k:  
18:      print(k, "wins")  
19:      return 1  
20:    if board[0][1] == k and board[1][1]==k and board[2][1]==k:  
21:      print(k, "wins")  
22:      return 1  
23:    if board[0][2] == k and board[1][2]==k and board[2][2]==k:  
24:      print(k, "wins")  
25:      return 1  
26:    else:  
27:      return 0  
28:  def play():  
29:    board=[[0,0,0],[0,0,0],[0,0,0]] #basic_board  
30:    for i in range(9):  
31:      print((i%2)+1,"Person's Move")  
32:      a,b=input().split()  
33:      a,b=[int(a),int(b)]  
34:      mark(board,a,b,(i%2)+1)  
35:      if check(board,(i%2)+1):  
36:        break  
37:    else:  
38:      print("you love ties! don't you?")  
39:  def mark(board,i,j,k):  
40:    board[i][j]=k  
41:    for i, j in [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]:  
42:      print(board[i][j],end=" ")  
43:      if j==2:  
44:        print("\n")  
45:  play()  
46:  print("Run again to play again! or ask Mohit to add a while loop with a break condition!")  

Comments

Popular Posts