From 723867ac65809578774c5ced68d34db49d7ff567 Mon Sep 17 00:00:00 2001 From: mnjx Date: Thu, 9 Mar 2023 21:33:59 -1000 Subject: [PATCH] fix: general bugfixes --- latest/tictactoe-latest.py | 86 +++++-------------- ...toe-v2.2-new.py => tictactoe-v2.2.1-new.py | 86 +++++-------------- 2 files changed, 44 insertions(+), 128 deletions(-) rename tictactoe-v2.2-new.py => tictactoe-v2.2.1-new.py (80%) diff --git a/latest/tictactoe-latest.py b/latest/tictactoe-latest.py index 895ac26..7542464 100644 --- a/latest/tictactoe-latest.py +++ b/latest/tictactoe-latest.py @@ -1,10 +1,8 @@ -# TODO - __DONE__ variable symbol count needed to win -# - __DONE__ selecting the position of the new symbol by moving a cursor with arrow keys inside the playfield, press space to place the symbol -# - __DONE__ when a game ends, the winning symbols will blink a few times +# TODO - -# add: variable symbol count needed to win, selecting the position of the new symbol by moving a cursor with arrow keys inside the playfield, press space to place the symbol, when a game ends, the winning symbols will blink a few times +# add: # fix: general bugfixes -# update: comments +# update: # algorithm steps # @@ -137,6 +135,8 @@ def _print(pf, name1, name2, blink = 0): print("o won!") elif blink == 3: print("x won!") + elif blink == 4: + print("Draw!") print(name1, "(o) vs", name2, "(x)") for i in range(len(pf)): @@ -175,6 +175,8 @@ def gameState(pf, name1, name2, blink = 0): _print(pf, name1, name2, 2) elif blink == 3: _print(pf, name1, name2, 3) + elif blink == 4: + _print(pf, name1, name2, 4) # checks whether someone has won def check(pf): @@ -255,106 +257,68 @@ def check(pf): for i in range(len(pf)): for j in range(len(pf)): if pf[i][j] == 'o': - try: - if pf[i-1][j-1] == 'o': - for k in range(symbolCount): - if pf[i-k][j-k] == 'o': - temp.append([i-k, j-k]) - o += 1 - else: - temp = [] - o = 0 - except IndexError: - continue - try: - if pf[i-1][j+1] == 'o': - for k in range(symbolCount): - if pf[i-k][j+k] == 'o': - temp.append([i-k, j+k]) - o += 1 - else: - temp = [] - o = 0 - except IndexError: - continue try: if pf[i+1][j-1] == 'o': for k in range(symbolCount): - if pf[i+k][j-k] == 'o': + if pf[i+k][j-k] == 'o' and 0 <= i+k < _size and 0 <= j-k < _size: temp.append([i+k, j-k]) o += 1 else: + winLine = [] temp = [] o = 0 except IndexError: - continue + pass try: if pf[i+1][j+1] == 'o': for k in range(symbolCount): - if pf[i+k][j+k] == 'o': + if pf[i+k][j+k] == 'o' and 0 <= i+k < _size and 0 <= j+k < _size: temp.append([i+k, j+k]) o += 1 else: + winLine = [] temp = [] o = 0 except IndexError: - continue + pass if o == symbolCount: winLine = temp return 0 elif o < symbolCount: + winLine = [] temp = [] o = 0 if pf[i][j] == 'x': - try: - if pf[i-1][j-1] == 'x': - for k in range(symbolCount): - if pf[i-k][j-k] == 'x': - temp.append([i-k, j-k]) - x += 1 - else: - temp = [] - x = 0 - except IndexError: - continue - try: - if pf[i-1][j+1] == 'x': - for k in range(symbolCount): - if pf[i-k][j+k] == 'x': - temp.append([i-k, j+k]) - x += 1 - else: - temp = [] - x = 0 - except IndexError: - continue try: if pf[i+1][j-1] == 'x': for k in range(symbolCount): - if pf[i+k][j-k] == 'x': + if pf[i+k][j-k] == 'x' and 0 <= i+k < _size and 0 <= j-k < _size: temp.append([i+k, j-k]) x += 1 else: + winLine = [] temp = [] x = 0 except IndexError: - continue + pass try: if pf[i+1][j+1] == 'x': for k in range(symbolCount): - if pf[i+k][j+k] == 'x': + if pf[i+k][j+k] == 'x' and 0 <= i+k < _size and 0 <= j+k < _size: temp.append([i+k, j+k]) x += 1 else: + winLine = [] temp = [] x = 0 except IndexError: - continue + pass if x == symbolCount: winLine = temp return 1 elif x < symbolCount: + winLine = [] temp = [] x = 0 @@ -457,24 +421,18 @@ def gameRound(pf, name1, name2): if won == 0: # 5.1 -> if yes -> end the game - clearScreen() gameState(pf, name1, name2, 1) - print("o won!") gameState(pf, name1, name2, 2) end = 1 # 5.2 -> if no -> jump to step 1 if won == 1: - clearScreen() gameState(pf, name1, name2, 1) - print("x won!") gameState(pf, name1, name2, 3) end = 1 if won == 2: - clearScreen() - print("Draw!") - gameState(pf, name1, name2) + gameState(pf, name1, name2, 4) end = 1 # driver code diff --git a/tictactoe-v2.2-new.py b/tictactoe-v2.2.1-new.py similarity index 80% rename from tictactoe-v2.2-new.py rename to tictactoe-v2.2.1-new.py index 895ac26..7542464 100644 --- a/tictactoe-v2.2-new.py +++ b/tictactoe-v2.2.1-new.py @@ -1,10 +1,8 @@ -# TODO - __DONE__ variable symbol count needed to win -# - __DONE__ selecting the position of the new symbol by moving a cursor with arrow keys inside the playfield, press space to place the symbol -# - __DONE__ when a game ends, the winning symbols will blink a few times +# TODO - -# add: variable symbol count needed to win, selecting the position of the new symbol by moving a cursor with arrow keys inside the playfield, press space to place the symbol, when a game ends, the winning symbols will blink a few times +# add: # fix: general bugfixes -# update: comments +# update: # algorithm steps # @@ -137,6 +135,8 @@ def _print(pf, name1, name2, blink = 0): print("o won!") elif blink == 3: print("x won!") + elif blink == 4: + print("Draw!") print(name1, "(o) vs", name2, "(x)") for i in range(len(pf)): @@ -175,6 +175,8 @@ def gameState(pf, name1, name2, blink = 0): _print(pf, name1, name2, 2) elif blink == 3: _print(pf, name1, name2, 3) + elif blink == 4: + _print(pf, name1, name2, 4) # checks whether someone has won def check(pf): @@ -255,106 +257,68 @@ def check(pf): for i in range(len(pf)): for j in range(len(pf)): if pf[i][j] == 'o': - try: - if pf[i-1][j-1] == 'o': - for k in range(symbolCount): - if pf[i-k][j-k] == 'o': - temp.append([i-k, j-k]) - o += 1 - else: - temp = [] - o = 0 - except IndexError: - continue - try: - if pf[i-1][j+1] == 'o': - for k in range(symbolCount): - if pf[i-k][j+k] == 'o': - temp.append([i-k, j+k]) - o += 1 - else: - temp = [] - o = 0 - except IndexError: - continue try: if pf[i+1][j-1] == 'o': for k in range(symbolCount): - if pf[i+k][j-k] == 'o': + if pf[i+k][j-k] == 'o' and 0 <= i+k < _size and 0 <= j-k < _size: temp.append([i+k, j-k]) o += 1 else: + winLine = [] temp = [] o = 0 except IndexError: - continue + pass try: if pf[i+1][j+1] == 'o': for k in range(symbolCount): - if pf[i+k][j+k] == 'o': + if pf[i+k][j+k] == 'o' and 0 <= i+k < _size and 0 <= j+k < _size: temp.append([i+k, j+k]) o += 1 else: + winLine = [] temp = [] o = 0 except IndexError: - continue + pass if o == symbolCount: winLine = temp return 0 elif o < symbolCount: + winLine = [] temp = [] o = 0 if pf[i][j] == 'x': - try: - if pf[i-1][j-1] == 'x': - for k in range(symbolCount): - if pf[i-k][j-k] == 'x': - temp.append([i-k, j-k]) - x += 1 - else: - temp = [] - x = 0 - except IndexError: - continue - try: - if pf[i-1][j+1] == 'x': - for k in range(symbolCount): - if pf[i-k][j+k] == 'x': - temp.append([i-k, j+k]) - x += 1 - else: - temp = [] - x = 0 - except IndexError: - continue try: if pf[i+1][j-1] == 'x': for k in range(symbolCount): - if pf[i+k][j-k] == 'x': + if pf[i+k][j-k] == 'x' and 0 <= i+k < _size and 0 <= j-k < _size: temp.append([i+k, j-k]) x += 1 else: + winLine = [] temp = [] x = 0 except IndexError: - continue + pass try: if pf[i+1][j+1] == 'x': for k in range(symbolCount): - if pf[i+k][j+k] == 'x': + if pf[i+k][j+k] == 'x' and 0 <= i+k < _size and 0 <= j+k < _size: temp.append([i+k, j+k]) x += 1 else: + winLine = [] temp = [] x = 0 except IndexError: - continue + pass if x == symbolCount: winLine = temp return 1 elif x < symbolCount: + winLine = [] temp = [] x = 0 @@ -457,24 +421,18 @@ def gameRound(pf, name1, name2): if won == 0: # 5.1 -> if yes -> end the game - clearScreen() gameState(pf, name1, name2, 1) - print("o won!") gameState(pf, name1, name2, 2) end = 1 # 5.2 -> if no -> jump to step 1 if won == 1: - clearScreen() gameState(pf, name1, name2, 1) - print("x won!") gameState(pf, name1, name2, 3) end = 1 if won == 2: - clearScreen() - print("Draw!") - gameState(pf, name1, name2) + gameState(pf, name1, name2, 4) end = 1 # driver code