Compare commits

...

11 Commits

Author SHA1 Message Date
oto
d329c0f7ab exposing my test suite. for u. to see. 2022-08-07 09:33:07 +03:00
oto
8bc369c31c a little reminder to myself 2022-08-07 09:32:16 +03:00
oto
e8236a7e4a made it remember its marks 2022-08-07 09:26:57 +03:00
oto
468237429e Switched to TKinter
Switched since the game has no mainloop, and only works when you commit an action. It makes sense, ye know. Maybe when i'll get some better graphics i'll switch to another lib, but for now, I'll use TKinter.
2022-08-07 09:26:13 +03:00
oto
ab37e797de elaborated a moment 2022-08-07 09:24:40 +03:00
oto
344f2e2909 added brackets to the player name 2022-08-07 09:24:27 +03:00
oto
bc3d3b59c9 Explained some stuff 2022-08-07 09:24:05 +03:00
oto
855e53befe Fixed a syntax error 2022-08-07 09:23:52 +03:00
oto
8dd2035167 Added win condition logic
It was the hardest to implement, but yay, I did it.
2022-08-07 09:23:10 +03:00
oto
a9b8409928 removed an excessive space 2022-08-07 09:22:16 +03:00
oto
5b74ecef52 Added default rules
Same as in regular classic TicTacToe.
2022-08-07 09:22:06 +03:00
6 changed files with 74 additions and 13 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
tester.py

View File

@ -5,14 +5,13 @@ from Player import *
class GameLogic(): class GameLogic():
def __init__(self, gameMap, playerList, winRowLength, individualMoves): def __init__(self, gameMap, playerList, winRowLength = 3, individualMoves = 1):
self.gameMap = gameMap self.gameMap = gameMap
self.playerList = playerList self.playerList = playerList
self.winRowLength = winRowLength self.winRowLength = winRowLength
self.individualMoves = individualMoves self.individualMoves = individualMoves
self.score = 0 self.score = 0
self.wait_for_input() self.wait_for_input()
@ -20,10 +19,32 @@ class GameLogic():
pass pass
def check_for_win(self, playerList): def check_who_win(self):
for player in playerList: # Should have used vectors instead of committing tuples mithosis,
pass # but whatever, it works anyway, and if it works, why code more
# for nothing?
for player in self.playerList:
markList = player.get_markList()
# since every mark is checked, there's no need to go into
# opposite directions, so, there is (0,1) but no (0, -1).
# This twice decreases the amount of computation needed.
# maybe TODO - Optimize even more by preventing repeated calculations.
directions = ((0, 1), (1, 0), (1,1), (-1, 1))
for mark in markList:
for direction in directions:
if self.is_line(markList, mark, direction):
return player
return False
def is_line(self, markList, mark, direction): # INTERNAL, I guess.
for i in range(1, self.winRowLength):
seeked = (mark[0] + direction[0] * i, mark[1] + direction[1] * i)
if seeked in markList:
continue
else:
return False
return True
def get_time(self): def get_time(self):
return str(datetime.now() - self.startTime) return str(datetime.now() - self.startTime)

View File

@ -18,15 +18,17 @@ class GameMap:
def add_mark(self, x, y, player): def add_mark(self, x, y, player):
# Can be 0 or can be a max value, so,
# this is border-inclusive.
if (x <= self.max_size_x and y <= self.max_size_y) and (x >= 0 and y >= 0): if (x <= self.max_size_x and y <= self.max_size_y) and (x >= 0 and y >= 0):
if (x, y) not in self.data: if (x, y) not in self.data:
self.data[(x,y)] = player self.data[(x,y)] = player
player.add_mark((x,y)) player.add_mark(x,y)
else: else:
errorMessage = "The mark is already busy by the Player " + str(self.data[(x, y)].get_name()) errorMessage = "The mark is already busy by the player '" + str(self.data[(x, y)].get_name()) + "'"
raise KeyError(errorMessage) raise KeyError(errorMessage)
else: else:
errorMessage = "The mark" + str((x,y)) + " is beyond preset max values, max_x - " + str(self.max_size_x) + ", max_y - " + str(self.max_size_y) errorMessage = "The mark" + str((x,y)) + " is beyond preset max values or 0, max_x - " + str(self.max_size_x) + ", max_y - " + str(self.max_size_y)
raise IndexError(errorMessage) raise IndexError(errorMessage)

View File

@ -1,2 +1,7 @@
import pygame from tkinter import *
from GameLogic import * from GameLogic import *
root = Tk()
root.title("Tic-Far-Toe v0.1")
root.mainloop()

View File

@ -8,9 +8,17 @@ class Player():
return "Player " + self.name + ': ' + "\n " + str(self.markList) return "Player " + self.name + ': ' + "\n " + str(self.markList)
def get_markList(self):
return self.markList
def get_name(self): def get_name(self):
return self.name return self.name
def add_mark(self, x, y): def add_mark(self, x, y):
self.markList.append((x, y)) self.markList.append((x, y))
def clear_mark_list():
self.markList = []

27
tester.py Normal file
View File

@ -0,0 +1,27 @@
from GameLogic import *
from unittest import *
gmap = GameMap(10, 10)
player1 = Player("first")
player2 = Player("second")
playerList = [player1, player2]
gmap.add_mark(4, 4, player1)
gmap.add_mark(4, 5, player1)
gmap.add_mark(4, 6, player1)
gmap.add_mark(10, 10, player2)
gmap = gameMap(5, 5)
print(gmap)
print(player1)
print(player2)
glogic = GameLogic(GameMap, playerList)
print(glogic.check_who_win())
# AAAAAAAAAAAA I AM GOING INSANE OVER THIS BULLSHIELD
# Outdated this message is, I just work with tuple[0] and tuple[1].
# ----
# Anyway, a reminder when you'll get to the web - figure out how to manipulate
# tuples (or replace them altogether) so they'll behave like math matrices -
# as in, (4, 4) * 2 -> (8, 8), and as in, (4, 4) + (-2, 3) -> (2, 7)