Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 is there a way to optimize my checking system?
#1
I am coding tic tac toe with lists (not 2d lists, this project was suggested by my teacher) and I want to go above and beyond, so I want to optimize my program as much as possible.

Right now, the part of the program that sticks out like a sore thumb, the checker.

Here is the code
def check(): #will check if Player One/Two has won
    R1CX = Row1.count("X") #side check
    R2CX = Row2.count("X")
    R3CX = Row3.count("X")
    R1CO = Row1.count("O")
    R2CO = Row2.count("O")
    R3CO = Row3.count("O")
    if R1CX == 3 or R2CX == 3 or R3CX == 3:
        print("X Wins!")
        
    if R1CO == 3 or R2CO == 3 or R3CO == 3:
        print("O Wins!")

        
    if Row1[2] == "X" and Row2[1] == "X" and Row3[0] == "X": #dia check
        print("X Wins!")
    if Row1[0] == "X" and Row2[1] == "X" and Row3[2] == "X":
        print("X Wins!")
        
    if Row1[2] == "O" and Row2[1] == "O" and Row3[0] == "O":
        print("O Wins!")
    if Row1[0] == "O" and Row2[1] == "O" and Row3[2] == "O":
        print("O Wins!")

    if Row1[0] == "X" and Row2[0] == "X" and Row3[0] == "X": #upanddown check
        print("X Wins!")
    if Row1[1] == "X" and Row2[1] == "X" and Row3[1] == "X":
        print("X Wins!")
    if Row1[2] == "X" and Row2[2] == "X" and Row3[2] == "X":
        print("X Wins!")

    if Row1[0] == "O" and Row2[0] == "O" and Row3[0] == "O": 
        print("O Wins!!")
    if Row1[1] == "O" and Row2[1] == "O" and Row3[1] == "O":
        print("O Wins!")
    if Row1[2] == "O" and Row2[2] == "O" and Row3[2] == "O":
        print("O Wins!")
By using count I saved myself a few lines but I don't think there is any way to check across lists (again, I have to use regular lists)

If anyone can point me in the right direction, that would be great! Thanks in advance.
Quote
#2
What do you mean by regular lists?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Oct-12-2019, 01:43 PM)ichabod801 Wrote: What do you mean by regular lists?

I mean not a list of lists

e.g.
a = [items]
b = [items]
c = [items]
Quote
#4
Are you required to use global variables, too?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
For checking the winner sets might be useful. Just general idea:

>>> first = 'xoo'
>>> second = 'xxx'
>>> third = 'oox'
>>> [row[0] for row in [first, second, third] if len(set(row)) == 1]  # for rows
['x']
>>> set([row[i] for i, row in enumerate([first, second, third])])     # for main diagonal                        
{'x'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
Question Difference between Python's os.system and Perl's system command Agile741 13 809 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Difference between os.system("clear") and os.system("cls") chmsrohit 6 2,741 Jun-16-2019, 11:38 AM
Last Post: DeaD_EyE
  cannot import scipy.optimize.Bounds larkypython 2 1,560 May-05-2019, 04:09 AM
Last Post: larkypython
  Optimize unittest loading Nazz 3 512 Mar-05-2019, 11:59 AM
Last Post: Nazz
  optimize choices in multiple if ---: statements Pedroski55 2 682 Dec-25-2018, 05:06 AM
Last Post: Pedroski55

Forum Jump:


Users browsing this thread: 1 Guest(s)