Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tic tac toe game
#1
I'm new to this forum so i hope someone can help me with this thread. Shy
The game is simple but I had to do several checks to make it work


shift = 0
game = [[],[],[]]


print("     0   1   2")
print("  ","-"*13)
for c in range(3):
	print(c, end='  | ')
	for l in range(3):
		game[c].append(" ")
		print(game[c][0],end=' | ')
	print()
	print("  ","-"*13)

def drawGame(player,x,y):
	system("clear")
	if player == 1:
		if " " in game[x][y]:
			game[x][y] = "X"
			shift += 1
		else:
			print("(?) You missed your shift.")
	elif player == 2:
		if " " in game[x][y]:
			game[x][y] = "O"
			shift += 1
		else:
			print("(?) You missed your shift.")
	print("="*18)
	print("{}     0   1   2{}".format(green,end))
	print("  ","-"*13)
	for c in range(3):
		print("{}{}{}".format(green,c,end), end='  | ')
		for l in range(3):
			print("{}{}{}".format(yellow,game[c][l],end),end=' | ')
		print()
		print("  ","-"*13)	

def winner():													 
	#Player1 <That is a problem>
	if game[0][0] == game[0][1] == game[0][2] == "X": return "Player1"					
	elif game[1][0] == game[1][1] == game[1][2] == "X": return "Player1"
	elif game[2][0] == game[2][1] == game[2][2] == "X": return "Player1"					
	elif game[0][0] == game[1][0] == game[2][0] == "X": return "Player1"					
	elif game[0][1] == game[1][1] == game[2][1] == "X": return "Player1"			
	elif game[2][0] == game[2][1] == game[2][2] == "X": return "Player1"
	elif game[0][0] == game[1][1] == game[2][2] == "X": return "Player1"
	elif game[2][0] == game[1][1] == game[0][2] == "X": return "Player1"
	elif game[0][2] == game[1][2] == game[2][2] == "X": return "Player1"
	#Player2
	elif game[0][0] == game[0][1] == game[0][2] == "O": return "Player2"					
	elif game[1][0] == game[1][1] == game[1][2] == "O": return "Player2"
	elif game[2][0] == game[2][1] == game[2][2] == "O": return "Player2"					
	elif game[0][0] == game[1][0] == game[2][0] == "O": return "Player2"					
	elif game[0][1] == game[1][1] == game[2][1] == "O": return "Player2"			
	elif game[2][0] == game[2][1] == game[2][2] == "O": return "Player2"
	elif game[0][0] == game[1][1] == game[2][2] == "O": return "Player2"
	elif game[2][0] == game[1][1] == game[0][2] == "O": return "Player2"
	elif game[0][2] == game[1][2] == game[2][2] == "O": return "Player2"
...
Reply
#2
What's your question?
Reply
#3
(Nov-01-2018, 08:43 PM)micseydel Wrote: What's your question?

thanks for answering
how can i make it more simple?
Reply
#4
Store all the winning sets of coordinates (as tuples) in a list. So one entry might be [(0, 0), (0, 1), (0, 2)]. To check for game end, check each winning set. If all the pieces at those coordinates are the same, the game is over. You don't have to check for both players, you can tell that by which piece is in one of the winning squares. Just make sure they're not all empty.

Use *args notation when printing rows: print('{}|{}|{}'.format(*game[0]).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Horizontal rows become
## instead of
if game[0][0] == game[0][1] == game[0][2] == "X": return "Player1"                  
elif game[1][0] == game[1][1] == game[1][2] == "X": return "Player1"
elif game[2][0] == game[2][1] == game[2][2] == "X": return "Player1"                    

## becomes
for x in range(3):
    if game[x][0]==game[x][1]==game[x][2]:
        if game[x][0]=="X":
            return "Player1"
        else:
            return "Player2"

## same for vertical
for y in range(3):
    if game[0][y]==game[1][y]==game[2][y]:
        if game[0][x]=="X":
            return "Player1"
        else:
            return "Player2" 
Reply
#6
as I did not think of this before Doh
thanks for the answers ichabod801 and woooee Thumbs Up
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020