Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions BasicPythonScripts/Tic-Tac-Toe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Title
Tic Tac Toe

## Short Description
A simple python script to play tic-tac-toe

## Setup instructions
For script to work you need to have python3 installed in your system. <br>
To run the script
```
python3 -u "tic_tac_toe.py"

```
## Detailed explanation
Tic-tac-toe (American English), noughts and crosses (Commonwealth English), or Xs and Os (Irish English) is a paper-and-pencil game for two players who take turns marking the spaces in a three-by-three grid with X or O. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. It is a solved game, with a forced draw assuming best play from both players.

## Output:-
<p align = "center">
<img src="Images/tic_tac_toe.png"/>
</p>

## Author(s):-
[Arjun M S](http://github.com/arjun-ms)
56 changes: 56 additions & 0 deletions BasicPythonScripts/Tic-Tac-Toe/tic_tac_toe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# print("Namste Duniya")

def sum(a, b, c):
return a + b + c


def printBoard(xState, zState):
one = 'X' if xState[1] else ('0' if zState[1] else 1)
two = 'X' if xState[2] else ('0' if zState[2] else 2)
three = 'X' if xState[3] else ('0' if zState[3] else 3)
four = 'X' if xState[4] else ('0' if zState[4] else 4)
five = 'X' if xState[5] else ('0' if zState[5] else 5)
six = 'X' if xState[6] else ('0' if zState[6] else 6)
senven = 'X' if xState[7] else ('0' if zState[7] else 7)
eight = 'X' if xState[8] else ('0' if zState[8] else 8)
nine = 'X' if xState[9] else ('0' if zState[9] else 9)
print(f"{one} | {two} | {three} ")
print(f"--|---|---")
print(f"{four} | {five} | {six} ")
print(f"--|---|---")
print(f"{senven} | {eight} | {nine} ")

def checkWin(xState, zState):
wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
for win in wins:
if(sum(xState[win[0]], xState[win[1]], xState[win[2]]) == 3):
print("X's win")
return 1
if(sum(zState[win[0]], zState[win[1]], zState[win[2]]) == 3):
print("O's win")
return 0
return -1



if __name__ == "__main__":
xState = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
zState = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
turn = 1 # 1for X and for 0
print("Welcome to tic tac toe")
while(True):
printBoard(xState, zState)
if(turn == 1):
print("X's Chance")
value = int(input("Please enter a value: "))
xState[value] = 1
else:
print("0's Chance")
value = int(input("Please enter a value: "))
zState[value] = 1
cwin = checkWin(xState, zState)
if(cwin != -1):
print("Match Over")
break

turn = 1 - turn