Python Forum
Tic tac toe win Condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic tac toe win Condition
#1
Hey I'd like to keep a majority of my code the same but my group is really having trouble making a win condition for my code can anybody help?

# tic tac toe
import random
import time

#initial gameboard 
txt = "[ 1 | 2 | 3 ] \n[ 4 | 5 | 6 ] \n[ 7 | 8 | 9 ]"
print(txt)
gamespaces = [1, 2, 3, 4, 5, 6, 7, 8, 9]
in_use = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#win function
xwin = []
owin = []
wincon1 = ["1", "2", "3"]
wincon2 = [4, 5, 6]
wincon3 = [7, 8, 9]
wincon4 = [1, 5, 9]
wincon5 = [3, 5, 7]
wincon6 = [1, 4, 7]
wincon7 = [2, 5, 8]
wincon8 = [3, 6, 9]
winpath1 =  all(elem in xwin  for elem in wincon1)
winpath2 =  all(elem in xwin  for elem in wincon2)
winpath3 =  all(elem in xwin  for elem in wincon3)
winpath4 =  all(elem in xwin  for elem in wincon4)
winpath5 =  all(elem in xwin  for elem in wincon5)
winpath6 =  all(elem in xwin  for elem in wincon6)
winpath7 =  all(elem in xwin  for elem in wincon7)
winpath8 =  all(elem in xwin  for elem in wincon8)

if winpath1:
  print("gamer win")

def winchk(checkwin):
  checkwin

#player input sequence
while True:
  inputvar = str(input("Pick a space: "))
  if inputvar in str(in_use):
    in_use.remove(int(inputvar))
    xwin.append(str(inputvar))
    Xreplace = txt.replace(inputvar, "x")
    txt = Xreplace
    #print(xwin)
    winchk(winpath1)
    winchk(winpath2)
    winchk(winpath3)
    winchk(winpath4)
    winchk(winpath5)
    winchk(winpath6) 
    winchk(winpath7)
    winchk(winpath8)
    aichoice = str(random.choice(in_use))
    in_use.remove(int(aichoice))
    owin.append(int(aichoice))
    Oreplace = txt.replace(aichoice, "O")
    txt = Oreplace
    print("robot chooses a space...")
    time.sleep(1)
    print(txt)
  else:
    print("that value won't work, choose:")
    for x in range(len(in_use)):
      print(in_use[x])
buran write Mar-18-2021, 07:21 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
(Mar-18-2021, 05:46 PM)Jacobthechosen Wrote: Hey I'd like to keep a majority of my code the same but my group is really having trouble making a win condition for my code can anybody help?

# tic tac toe
import random
import time

#initial gameboard
txt = "[ 1 | 2 | 3 ] \n[ 4 | 5 | 6 ] \n[ 7 | 8 | 9 ]"
print(txt)
gamespaces = [1, 2, 3, 4, 5, 6, 7, 8, 9]
in_use = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#win function
xwin = []
owin = []
wincon1 = ["1", "2", "3"]
wincon2 = [4, 5, 6]
wincon3 = [7, 8, 9]
wincon4 = [1, 5, 9]
wincon5 = [3, 5, 7]
wincon6 = [1, 4, 7]
wincon7 = [2, 5, 8]
wincon8 = [3, 6, 9]
winpath1 = all(elem in xwin for elem in wincon1)
winpath2 = all(elem in xwin for elem in wincon2)
winpath3 = all(elem in xwin for elem in wincon3)
winpath4 = all(elem in xwin for elem in wincon4)
winpath5 = all(elem in xwin for elem in wincon5)
winpath6 = all(elem in xwin for elem in wincon6)
winpath7 = all(elem in xwin for elem in wincon7)
winpath8 = all(elem in xwin for elem in wincon8)

if winpath1:
print("gamer win")

def winchk(checkwin):
checkwin

#player input sequence
while True:
inputvar = str(input("Pick a space: "))
if inputvar in str(in_use):
in_use.remove(int(inputvar))
xwin.append(str(inputvar))
Xreplace = txt.replace(inputvar, "x")
txt = Xreplace
#print(xwin)
winchk(winpath1)
winchk(winpath2)
winchk(winpath3)
winchk(winpath4)
winchk(winpath5)
winchk(winpath6)
winchk(winpath7)
winchk(winpath8)
aichoice = str(random.choice(in_use))
in_use.remove(int(aichoice))
owin.append(int(aichoice))
Oreplace = txt.replace(aichoice, "O")
txt = Oreplace
print("robot chooses a space...")
time.sleep(1)
print(txt)
else:
print("that value won't work, choose:")
for x in range(len(in_use)):
print(in_use[x])

I don't understand what I'm doing wrong
Reply
#3
You don't understand functions.

This creates a variable that is True or False:
winpath1 = all(elem in xwin for elem in wincon1)
This does not cause winpath1 to be re-evaluated. winpath1 has the same value it was assigned at the top of the module.
winchk(winpath1)
Not that it matters, because you never use the result of winchk anyway.

Your win function (yes, there should be a function) should look something like this:
Output:
def win(player): wins = ["123", "456", "789", "159", "357", "147", "258", "369"] for win in wins: if all(spot in player for spot in win): return True return False
Checking for a winner looks like this:
        if win(xwin):
            print(txt)
            print("You win!")
            break;
I used strings for the win combinations. When I put my win function in your code, I could win, but the robot couldn't. When I looked at xwin I saw ["1", "3"] and when I looked at owin I saw [2, 8]. Both the player and robot moves need to be saved using the same type. You can make the code work using int or str, but they both need to be the same, and you should use the same type in "in_use". I would use strings for everything, then you don't have to do any type conversion and your "in_use" check does all the input validation you need.

Good variable names go a long way in making code easier to understand. "in_use" is a really bad variable name for empty spots on a tic-tac-toe board. How about "available" or "empty"? "txt" is a bad name for your tic-tac-toe board. How about "board"?

Why is the player choice called "inputvar" and the robot choice called "aichoice". I would use the same variable for both ("choice", "move", "spot"?) because they are never used at the same time, but if you have two variables that do the same thing, their names should be similar. "playerchoice" and "aichoice"?

There is no reason for the temporary variables Oreplace or Xreplace:
Oreplace = txt.replace(aichoice, "O")
txt = Oreplace
I think it should be:
board = board.replace(choice, "O")
I really like the idea of the poorly named "in_use" and "txt" though. Very clever way to keep the board state in a way that is easy to display, and also a great way to simplify the robot choice. The owin and xwin lists are also quite clever. Lots of good ideas. You just need to work on the programming nuts and bolts part.
Reply
#4
This will test for a win but you also need to test for a tie. You might consider testing input also to make sure that a wrong input doesn't crash the script.

# tic tac toe
import random
import time

#initial gameboard
txt = "[ 1 | 2 | 3 ] \n[ 4 | 5 | 6 ] \n[ 7 | 8 | 9 ]"
print(txt)
gamespaces = [1, 2, 3, 4, 5, 6, 7, 8, 9]
in_use = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#win function
xwin = []
owin = []
wincon = [['1', '2', '3'],
		['4', '5', '6'],
		['7', '8', '9'],
		['1', '5', '9'],
		['3', '5', '7'],
		['1', '4', '7'],
		['2', '5', '8'],
		['3', '6', '9']]

def winchk(checkwin):
	for combination in wincon :
		count = 0
		for space in checkwin :
			if space in combination :
				count += 1
			if count == 3 :
				return True
	return False

#player input sequence
while True:
	inputvar = str(input("Pick a space: "))
	if inputvar in str(in_use):
		in_use.remove(int(inputvar))
		xwin.append(str(inputvar))
		if winchk (xwin) :
			print ('\nYou won!')
			break
		Xreplace = txt.replace(inputvar, "x")
		txt = Xreplace
		aichoice = str(random.choice(in_use))
		in_use.remove(int(aichoice))
		owin.append(str(aichoice))
		Oreplace = txt.replace(aichoice, "O")
		txt = Oreplace
		print("robot chooses a space...")
		time.sleep(1)
		print(txt)
		print (owin)
		if winchk (owin) :
			print ('Sorry, AI won :(')
			break
	else:
		print("that value won't work, choose:")
		for x in range(len(in_use)):
			print(in_use[x])
Reply


Forum Jump:

User Panel Messages

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