Home > Software design >  4 in a row game
4 in a row game

Time:12-16

Started a programing in python course in school, so pretty new to python. I've made a basic 4 in a row game with a play area of 4x4. But I don't understand how or why my winning system vertically isn't working as I intended.

It currently prints win if there are X and O next to each other in whatever row. Please don't comment any too complicated. Any improvements are appreciated.

 . | . | . | . 
 . | . | . | . 
 . | . | . | . 
 X | O | . | . 


import os
def cls():
    os.system('cls')

def rows():
    cls()
    print(*row4, sep=" | ")
    print(*row3, sep=" | ")
    print(*row2, sep=" | ")
    print(*row1, sep=" | ")

def check_ver():
    for x in range(4):
        for j in range(4):
            if ("X" in row1[j][x]) & ("X" in row2[j][x]) & ("X" in row3[j][x]) & ("X" in row4[j][x]):
                print("X, Win")
                break
            else:
                print("Fel igen!")

row1 = ['.', '.', '.', '.']
row2 = ['.', '.', '.', '.']
row3 = ['.', '.', '.', '.']
row4 = ['.', '.', '.', '.']


while True:
    rows()
    check_ver()
    col = int(input('X, which column? '))
    cls()
    if col == "":
        break
    elif col >= 5 or col < 1:
        print("Error")
        continue
    else:
        col = col - 1
        if row1[col] == '.':
            row1[col] = 'X'
            rows()
        else:
            if row2[col] == '.':
                row2[col] = 'X'
                rows()
            else:
                if row3[col] == '.':
                    row3[col] = 'X'
                    rows()
                else:
                    if row4[col] == '.':
                        row4[col] = 'X'
                        rows()
    
    col = int(input('O, which column? '))
    if col >= 5 or col < 1:
        print("Error")
        continue
    else:
        col = col - 1
        if row1[col] == '.':
            row1[col] = 'O'
            rows()
        else:
            if row2[col] == '.':
                row2[col] = 'O'
                rows()
            else:
                if row3[col] == '.':
                    row3[col] = 'O'
                    rows()
                else:
                    if row4[col] == '.':
                        row4[col] = 'O'
                        rows()

CodePudding user response:

The problem is that you are considering your list as a bidimensional one, but it isn't. You are using 4 different lists. So you don't need j in general. You should use something like this.

def check_ver():
    for x in range(4):
        if ("X" in row1[x]) & ("X" in row2[x]) & ("X" in row3[x]) & ("X" in row4[x]):
            print("X, Win")
            break
        else:
            print("Fel igen!")

row1 = ['X', '.', '.', '.']
row2 = ['X', '.', '.', '.']
row3 = ['X', '.', '.', '.']
row4 = ['X', '.', '.', '.']

Just a tip, use == operator instead of the in operator

def check_ver():
    for x in range(4):
        if (row1[x] == "X") & (row2[x] == "X") & (row3[x] == "X") & (row4[x] == "X"):
            print("X, Win")
            break
        else:
            print("Fel igen!")

row1 = ['X', '.', '.', '.']
row2 = ['X', '.', '.', '.']
row3 = ['X', '.', '.', '.']
row4 = ['X', '.', '.', '.']

Here is an example with a bidimensional list

def check_ver():
    for x in range(4):
        xCounter = 0
        for y in range(4):
            if (grid[y][x] == "X"):
                xCounter  = 1
        if xCounter == 4:
            print("X, Win")
            break
        else:
            print("Fel igen!")

grid = [['.', 'X', '.', '.'],
        ['.', 'X', '.', '.'],
        ['.', 'X', '.', '.'],
        ['.', 'X', '.', '.']]

  • Related