Example #1
0
    def stats(self, name):
        # Get player ID, return if not found
        pid = cPlayerHandler.getPID(self, name)
        if pid == -1:
            print "No such player found."
            return

        pData = cPlayerHandler.getData(self, pid)
        printf(
            "The player %s has played %i games, of which they have won %i and lost %i. They have quit %i times.\n",
            pData[0],
            pData[1],
            pData[2],
            pData[3],
            pData[4],
        )
Example #2
0
    def leaderboard(self, strMode):
        mode = -1

        if strMode[0] == "g":
            mode = 1  # games
            strMode = "played"

        elif strMode[0] == "w":
            mode = 2  # wins
            strMode = "won"

        elif strMode[0] == "l":
            mode = 3  # losses
            strMode = "lost"

        else:
            print "An error occured."
            return

        data = []

        # Retrieve player data
        for pid in range(cPlayerHandler.getPlayers(self)):
            data.append(cPlayerHandler.getData(self, pid))

        sort2d(data, mode)

        printf("Leaderboard in order of games %s.\n", strMode)

        for player in range(len(data)):
            printf(
                "%i. %-15.15s %i games played, %i games won, %i games lost, %i quits.\n",
                player + 1,
                str(data[player][0]) + ":",
                data[player][1],
                data[player][2],
                data[player][3],
                data[player][4],
            )
Example #3
0
    def newGame(self):

        pid1, pid2 = 0, 0

        while 1:
            mode = raw_input("Enter 1 for singleplayer, 2 for multiplayer: ")
            try:
                mode = int(mode)
            except ValueError:
                print "Please, enter 1 or 2"
                continue

            if mode == 1:
                pid1 = 0

                while 1:
                    pName = raw_input("Enter your name: ")
                    if pName != "" and pName.lower() != "cpu":
                        break
                    else:
                        print "Try again"

                pid2 = cPlayerHandler.getPID(self, pName)
                if pid2 == -1:
                    print "No such player found. Creating.."
                    pid2 = cPlayerHandler.addPlayer(self, pName)
                break

            elif mode == 2:
                while 1:
                    p1name = raw_input("Player 1, enter your name: ")
                    if p1name != "" and p1name.lower() != "cpu":
                        break
                    else:
                        print "Try again"

                pid1 = cPlayerHandler.getPID(self, p1name)
                if pid1 == -1:
                    print "No such player found. Creating.."
                    pid1 = cPlayerHandler.addPlayer(self, p1name)

                while 1:
                    p2name = raw_input("Player 2, enter your name: ")
                    if p2name != "" and p2name.lower() != "cpu":
                        break
                    else:
                        print "Try again"

                pid2 = cPlayerHandler.getPID(self, p2name)
                if pid2 == -1:
                    print "No such player found. Creating.."
                    pid2 = cPlayerHandler.addPlayer(self, p2name)

                break

            print "Please, enter 1 or 2"  # If mode wasn't 1 or 2, loop again.

        sleep(0.5)  # Why sleep? Because why not?

        status = cGame.__init__(self, pid1, pid2)  # Start new game

        if status == -1 or status == 2:
            print "Game ended to a draw"
        elif status == 0 or status == 1:
            printf("%s won the game\n", cGame.getNames(self)[status])
Example #4
0
    def turn(self, player):
        pid = self.players[player][0]
        if pid == 0:  # CPU
            print "CPU's turn"

            if self.twoInaRow(0) == True:
                return
            elif self.twoInaRow(1) == True:
                return
            else:  # We cannot win nor block, time for random placement
                # Generate random arrays of rows and
                rows = sample(range(3), 3)
                cols = sample(range(3), 3)
                row = 0
                col = 0

                valid = False

                for i in range(3):
                    for j in range(3):
                        row = rows[i]
                        col = cols[j]
                        if self.gameGrid[row][col] == " ":
                            valid = True
                            break
                    if valid == True:
                        break

                self.gameGrid[row][col] = self.players[player][2]

        else:  # non-cpu
            valid = False
            while valid == False:
                # Get input from player
                printf("%s, your turn (eg. A2, s to stop): ", self.players[player][1])
                temp = raw_input()
                temp = temp.upper()

                try:
                    if temp[0] == "S":
                        self.stop(-1)  # Stopcode for user quit
                        return
                except IndexError:
                    pass

                try:
                    pos = [temp[0], temp[1]]
                except IndexError:
                    valid = False
                    print "Invalid input!"
                    continue

                # Check for validity
                if not pos[0] in ["A", "B", "C"] or not pos[1] in ["1", "2", "3"]:
                    print "Invalid input!"
                    valid = False
                    continue

                # Convert
                else:
                    if pos[0] == "A":
                        pos[0] = 0
                    elif pos[0] == "B":
                        pos[0] = 1
                    else:
                        pos[0] = 2

                    pos[1] = int(pos[1]) - 1  # list indices start from 0
                    valid = True

                # Check if cell is free
                if self.gameGrid[pos[1]][pos[0]] == " " and valid == True:
                    valid = True
                else:
                    print "Position already taken!"
                    valid = False

            self.gameGrid[pos[1]][pos[0]] = self.players[player][2]
Example #5
0
 def printGame(self):
     clearScr()
     printf("   # A | B | C \n")
     printf("###############\n")
     printf(" 1 # %s | %s | %s \n", self.gameGrid[0][0], self.gameGrid[0][1], self.gameGrid[0][2])
     printf("---#-----------\n")
     printf(" 2 # %s | %s | %s \n", self.gameGrid[1][0], self.gameGrid[1][1], self.gameGrid[1][2])
     printf("---#-----------\n")
     printf(" 3 # %s | %s | %s \n\n", self.gameGrid[2][0], self.gameGrid[2][1], self.gameGrid[2][2])