how to can open the .py file like an application on windows? - python-3.x

from tkinter import *
import random
command_list = []
def next_turn(r, c):
global player
global command_list
global game_started
command_list.append((r, c))
if buttons[r][c]['text'] == "" and check_winner() is False:
if player == players[0]:
buttons[r][c]['text'] = player
game_started = True
if check_winner() is False:
player = players[1]
player_label.configure(text=(player + "'s turn"))
elif check_winner() is True:
player_label.configure(text=(player + " WINS!"))
elif check_winner() == 'Tie':
player_label.configure(text='TIE!')
else:
buttons[r][c]['text'] = player
if check_winner() is False:
player = players[0]
player_label.configure(text=(player + "'s turn"))
elif check_winner() is True:
player_label.configure(text=(player + " WINS!"))
elif check_winner() == 'Tie':
player_label.configure(text='TIE!', bg='yellow')
def check_winner():
# rows wise
for r in range(3):
if buttons[r][0]['text'] == buttons[r][1]['text'] == buttons[r][2]['text'] != "":
buttons[r][0].configure(bg='green')
buttons[r][1].configure(bg='green')
buttons[r][2].configure(bg='green')
return True
# column wise
for c in range(3):
if buttons[0][c]['text'] == buttons[1][c]['text'] == buttons[2][c]['text'] != "":
buttons[0][c].configure(bg='green')
buttons[1][c].configure(bg='green')
buttons[2][c].configure(bg='green')
return True
# diagonal wise
if buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != "":
buttons[0][0].configure(bg='green')
buttons[1][1].configure(bg='green')
buttons[2][2].configure(bg='green')
return True
if buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != "":
buttons[0][2].configure(bg='green')
buttons[1][1].configure(bg='green')
buttons[2][0].configure(bg='green')
return True
if empty_space():
return False
return "Tie"
def empty_space():
for r in range(3):
for c in range(3):
if buttons[r][c]['text'] == '':
return True
return False
def new_game():
global player
player = random.choice(players)
player_label.configure(text=player + "'s turn")
for r in range(3):
for c in range(3):
buttons[r][c].configure(text="", bg='grey')
def undo():
global player
if check_winner() is False and len(command_list) != 0:
undo_row, undo_column = command_list[-1][0], command_list[-1][1]
buttons[undo_row][undo_column]['text'] = ''
if player == players[1]:
player = players[0]
player_label.configure(text=(player + "'s turn"))
else:
player = players[1]
player_label.configure(text=(player + "'s turn"))
command_list.pop()
mainWindow = Tk()
mainWindow.title("Tic-Tac-Toe")
mainWindow.configure(background='grey')
players = ["X", "O"]
# pick a random player.
player = random.choice(players)
# end button
end_button = Button(mainWindow, text="Close", background='red', font=('consolas', 20), command=mainWindow.destroy)
end_button.pack(anchor='w')
player_label = Label(mainWindow, text=player + "'s turn", font=('consolas', 40))
player_label.pack(side='top')
reset_button = Button(mainWindow, text='New Game', font=('consolas', 20), command=new_game)
reset_button.pack(side='top')
# undo button
undo_button = Button(mainWindow, text='Undo', font=('consolas', 20), command=undo)
undo_button.pack(side='top', anchor='e')
# Buttons structure
buttons = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# Game Board.
board = Frame(mainWindow)
board.configure(background='grey')
board.pack()
for row in range(3):
for column in range(3):
buttons[row][column] = Button(board, text="", font=('consolas', 40), width=5, height=2,
command=lambda bt_row=row, bt_column=column:
next_turn(bt_row, bt_column))
buttons[row][column].grid(row=row, column=column)
buttons[row][column].configure(background='grey')
mainWindow.mainloop()
The above program is a game called Tic-Tak-Toe.
I am able to run the program on IDE without any error.
This is the program that i want to run on windows or linux or any other platform without using the IDE or command line, just like other application such as word, execel , halo 3(game) etc. How can I do it ?

You need to target an operating system and create an executable file. For example you could create a c program that calls your .py pgoram. Below is an example of how to do that for windows.
For windows - I used windows Ubuntu linux subsystem to create the executable targeting windows 64-bit:
sudo apt-get install mingw-w64. Read more.
make new file game.c
#include <stdlib.h>
int main() {
system("py ./temp.py");
return 0;
}
Put your game.py and game.c in the same folder and run
x86_64-w64-mingw32-gcc -o game.exe game.c
Working Picture

Related

Py-Game Connect4 - Minimax (Program Halting Recursively)

main.py
from Player import Player
import tkinter as tk
import pygame
import pygame_menu
import time
import colors
import Connect4 as cFour
import Minimax as mx
def text_format(option, textSize, textColor):
"""
Creates a text object to show in the main menu
"""
newFont = pygame.font.Font(pygame_menu.font.FONT_FRANCHISE, textSize)
newText = newFont.render(option, 0, textColor)
return newText
def load_screen():
"""
This initializes the window for pygame to use
"""
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Connect4")
return screen
def get_player_details(screen):
"""
Creates a tkinter object(button) that gets players names
"""
root = tk.Tk()
root.title("Player Names!")
tk.Label(root, text="Player One", fg="blue").grid(row=0)
tk.Label(root, text="Player Two", fg="red").grid(row=1)
p1 = tk.Entry(root, font=(None, 15))
p2 = tk.Entry(root, font=(None, 15))
p1.grid(row=0, column=1)
p2.grid(row=1, column=1)
tk.Button(root, text='Play!', command= lambda: play_game(p1.get(),p2.get(), root, screen)).grid(row=10, column=1, sticky=tk.W)
tk.mainloop()
def get_player_ai_details(screen):
"""
Creating the panel to allow the user to select a color and go against the AI
"""
options = ["Player 1", "Player 2"]
root = tk.Tk()
root.title("Player 1(Blue) or 2(Red)?")
colorChoice= tk.StringVar(root)
colorChoice.set(options[0])
tk.OptionMenu(root, colorChoice, *options).grid(row=3)
p1 = tk.Entry(root, font=(None, 15))
p1.grid(row=3, column=1)
tk.Button(root, text="Play Computer!", command=lambda: play_computer(colorChoice.get(), p1.get(), root, screen)).grid(row=10, column=1)
tk.mainloop()
def play_computer(colorChoice, playerName, root, screen):
"""
Connect4 play function (human v computer)
"""
root.destroy()
if colorChoice == "Player 1":
mx.Minimax(Player(playerName), Player("Ed"), screen).play_computer()
else:
mx.Minimax(Player("Ed"), Player(playerName), screen).play_computer()
def play_game(p1Name, p2Name, root, screen):
"""
Connect4 play function (human v human)
"""
root.destroy()
game = cFour.Connect4(Player(p1Name.strip()), Player(p2Name.strip()), screen).play()
if __name__ == "__main__":
pygame.init()
screen = load_screen()
features = [
("Player Vs Player", colors.yellow),
("Player Vs AI", colors.red),
("Quit", colors.gray)
]
iterator = 0
menu = True
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#This if block makes it where the user doesnt have to click arrow key up/down if they have exhausted the possible options, it will loop you throughout options
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
iterator += 1
if iterator == len(features):
iterator = 0
if event.key == pygame.K_UP:
iterator -= 1
if iterator < 0:
iterator = len(features) - 1
if event.key == pygame.K_RETURN:
if selected == "Player Vs Player":
get_player_details(screen)
if selected == "Player Vs AI":
get_player_ai_details(screen)
if selected == "Quit":
pygame.quit()
quit()
selected = features[iterator][0]
screen.fill(colors.blue)
screen_rect = screen.get_rect()
for i in range(0, len(features)):
counter = -50 + (i * 90) # Equation that sets distance between each choice in main menu
if i == iterator:
text = text_format(features[i][0], 80, features[i][1])
else:
text = text_format(features[i][0], 80, colors.black)
player_rect = text.get_rect(center=screen_rect.center)
player_rect[1] = player_rect[1] + counter
screen.blit(text, player_rect)
pygame.display.update()
Connect4.py
import pygame
import colors
import tkinter as tk
import pygame_menu
# import pandas as pd
import random
class Connect4:
"""
Class used to represent connect4 game
"""
def __init__(self, player1, player2, screen):
# Use 1 version of the screen instead of trying to create a new one
self.screen = screen
# Circle Radius and Width
self.WIDTH = 0
self.CIRCLERADIUS = 25
# Game-Time Variables
self.player1 = player1
self.player2 = player2
self.moveNumber = 0
self.gameOver = False
self.COLUMNS = 7
self.ROWS = 6
self.EMPTY = 99
self.board = [[self.EMPTY for x in range(self.COLUMNS)] for y in range(self.ROWS)]
# The distance between where the window starts and the game board is placed
self.DISTANCE = 90
# Space between each circle
self.DISTANCEGAP = 70
# Setting rectangle default
self.LEFT = 50
self.TOP = 70
self.HEIGHT = 470
self.RECWIDTH = 500
#Creating new tkinterobject
self.root = tk.Tk()
self.scoreboard = {self.player1.name: 0, self.player2.name: 0, "ties": 0}
# Storing locations of available moves given a user clicks the window -- Tuple of locations
self.POSITIONS = [
(
self.DISTANCE + (self.DISTANCEGAP*column) - self.CIRCLERADIUS,
self.DISTANCE + (self.DISTANCEGAP*column) + self.CIRCLERADIUS
)
for column in range(0, self.COLUMNS)
]
def who_won(self, board, piece):
"""
Determines the state of the game and finds if there is a winner
"""
# Horizontal
for col in range(0, self.COLUMNS - 3):
for row in range(0, self.ROWS):
if board[row][col] == piece and board[row][col + 1] == piece and board[row][col + 2] == piece and board[row][col + 3] == piece:
return True
# Vertical
for col in range(0, self.COLUMNS):
for row in range(0, self.ROWS - 3):
if board[row][col] == piece and board[row + 1][col] == piece and board[row + 2][col] == piece and board[row + 3][col] == piece:
return True
# Up-Left/Down-Right
for col in range(3, self.COLUMNS):
for row in range(3, self.ROWS):
if board[row][col] == piece and board[row - 1][col - 1] == piece and board[row - 2][col - 2] == piece and board[row - 3][col - 3] == piece:
return True
# Up-Right/Down-Left
for col in range(0, self.COLUMNS - 3):
for row in range(3, self.ROWS):
if board[row][col] == piece and board[row - 1][col + 1] == piece and board[row - 2][col + 2] == piece and board[row - 3][col + 3] == piece:
return True
# A winning move is not found
return False
def is_legal_move(self, position, board):
"""
Validates if a move is available/legal
"""
if board[0][position] == self.EMPTY:
return True
return False
def display_board(self):
"""
Displaying the game board to the user
"""
# Function: rect(surface, color, rectangle object, optional width) -- First one forms the outline of the board
pygame.draw.rect(self.screen, colors.salmon, (self.LEFT, self.TOP, self.RECWIDTH, self.HEIGHT), 13)
# This forms inner-most rectangle that users play on
pygame.draw.rect(self.screen, colors.burlywood, (self.LEFT, self.TOP, self.RECWIDTH, self.HEIGHT))
for column in range(0, self.COLUMNS):
colEq = self.DISTANCE + (self.DISTANCEGAP * column)
for row in range(0, self.ROWS):
# 125 is used here to make a the board placed in the center of the board and helps finding a value for self.TOP easier
rowEq = 125 + (self.DISTANCEGAP * row)
if self.board[row][column] == self.EMPTY:
color = colors.white
elif self.board[row][column] == 0:
color = colors.realBlue
elif self.board[row][column] == 1:
color = colors.red
pygame.draw.circle(self.screen, color, (colEq, rowEq), self.CIRCLERADIUS, self.WIDTH)
pygame.display.flip()
def play(self):
"""
This is the game-loop
"""
while not self.gameOver:
self.display_board()
if self.moveNumber % 2 == 0:
userText, userRect = self.display_player_name(self.player1.name, colors.realBlue)
elif self.moveNumber % 2 == 1:
userText, userRect = self.display_player_name(self.player2.name, colors.red)
self.screen.blit(userText, userRect)
for event in pygame.event.get():
self.screen.fill(colors.aquamarine) # Set up background color
if event.type == pygame.QUIT:
self.gameOver = True
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
position = self.get_column_position(x)
if self.moveNumber % 2 == 0 and position != self.EMPTY:
if self.is_legal_move(position, self.board):
self.drop_piece_animation(position)
if self.who_won(self.board, 0):
self.gameOver = True
self.scoreboard[self.player1.name] = self.scoreboard.get(self.player1.name) + 1
userText, userRect = self.display_player_name(self.player1.name + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
elif self.moveNumber % 2 == 1 and position != self.EMPTY:
if self.is_legal_move(position, self.board):
self.drop_piece_animation(position)
if self.who_won(self.board, 1):
self.gameOver = True
self.scoreboard[self.player2.name] = self.scoreboard.get(self.player2.name) + 1
userText, userRect = self.display_player_name(self.player2.name + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
self.display_board()
self.screen.blit(userText, userRect)
pygame.display.flip()
self.display_scoreboard(False)
def display_scoreboard(self, isAi):
"""
This enables the tkinter object so I can display the user options after : Victory/Loss/Tie
"""
self.root.geometry('460x150+300+0')
self.reset()
self.root.title("Choices")
# This creates the feedback information screen that the user sees after a game
tk.Label(self.root, text="Close window to go to main menu", font=(None, 15, 'underline'), anchor='w', justify='left').grid(row=0, column=1, sticky="NSEW")
tk.Label(self.root, text=self.player1.name + ": " + str(self.scoreboard.get(self.player1.name)), font=(None, 15), anchor='w', justify='left').grid(row=1, column=1, sticky = "NSEW")
tk.Label(self.root, text=self.player2.name + ": " + str(self.scoreboard.get(self.player2.name)), font=(None, 15), anchor='w', justify='left').grid(row=2, column=1, sticky="NSEW")
tk.Label(self.root, text="Ties: " + str(self.scoreboard.get("ties")), font=(None, 15), anchor='w', justify='left').grid(row=3, column=1, sticky="NSEW")
# if isAi == True:
# # tk.Button(self.root, text='Rematch!', command=self.playAi, font=(None, 12), fg="blue").grid(row=4, column=1, sticky=tk.W)
# else:
tk.Button(self.root, text='Rematch!', command=self.play, font=(None, 12), fg="blue").grid(row=4, column=1, sticky=tk.W)
# tk.Button(self.root, text='Rematch with Swap!', command= lambda: self.swapPlayers(isAi), font=(None, 12), fg="red").grid(row=4, column=2, sticky=tk.W)
tk.Entry(self.root)
self.root.mainloop()
def check_if_tie(self, board):
"""
A possible game state : Checking for a tie
"""
totalPieces = 0
for col in range(0, self.COLUMNS):
for row in range(0, self.ROWS):
if board[row][col] == 0 or board[row][col] == 1:
totalPieces += 1
if totalPieces == 42:
return True
else:
return False
def display_player_name(self, name, color):
"""
A feature to help users know who's turn it is that gets displayed
"""
font = pygame.font.Font(pygame_menu.font.FONT_FRANCHISE, 60)
text = font.render(name, True, color)
textRect = text.get_rect()
textRect.center = (len(name) * 30, 20)
return text, textRect
def drop_piece_animation(self, position):
"""
Inserting a piece at a given position with the animation of a piece drop
"""
tmpRow = 5
while self.board[tmpRow][position] == 1 or self.board[tmpRow][position] == 0:
tmpRow -= 1
for i in range(0, tmpRow + 1):
self.board[i][position] = self.moveNumber % 2
self.display_board()
pygame.time.delay(200)
pygame.display.flip()
self.board[i][position] = self.EMPTY
self.board[tmpRow][position] = self.moveNumber % 2
self.moveNumber += 1
def get_column_position(self, position):
"""
Takes a X coordinate value dependent on a click and determines what column user clicked
"""
index = 0
for i in self.POSITIONS:
if position + self.CIRCLERADIUS/2 >= i[0] and position - self.CIRCLERADIUS/2 <= i[1]:
return index
index += 1
return self.EMPTY
def reset(self):
"""
Restoring the game in its original state
"""
self.moveNumber = 0
self.board = [[self.EMPTY for x in range(self.COLUMNS)] for y in range(self.ROWS)]
self.gameOver = False
def play_computer(self):
"""
This is the game-loop used for AI play
"""
# If/else block to distinguish the human/Ai because the ai cant mouse click events
if self.player1.name == "Ed": # Ed Watkins (Staten Island)
humanPlayer = 1
computerPlayer = 0
humanName = self.player2.name
computerName = self.player1.name
elif self.player2.name == "Ed":
humanPlayer = 0
computerPlayer = 1
humanName = self.player1.name
computerName = self.player2.name
while not self.gameOver:
self.display_board()
if self.moveNumber % 2 == 0:
userText, userRect = self.display_player_name(self.player1.name, colors.blue)
elif self.moveNumber % 2 == 1:
userText, userRect = self.display_player_name(self.player2.name, colors.red)
self.screen.blit(userText, userRect)
for event in pygame.event.get():
self.screen.fill(colors.aquamarine) # Set up background color
if event.type == pygame.QUIT:
self.gameOver = True
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
position = self.get_column_position(x)
if self.moveNumber % 2 == humanPlayer and position != self.EMPTY:
if self.is_legal_move(position, self.board):
self.drop_piece_animation(position)
if self.who_won(self.board, humanPlayer):
self.gameOver = True
self.scoreboard[humanName] = self.scoreboard.get(humanName) + 1
userText, userRect = self.display_player_name(humanName + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
if self.moveNumber % 2 == computerPlayer and self.gameOver == False:
move = self.generate_move(self.board, 4, computerPlayer, humanPlayer, True, self.moveNumber)
self.drop_piece_animation(move)
if self.who_won(self.board, computerPlayer):
self.gameOver = True
self.scoreboard[computerName] = self.scoreboard.get(computerName) + 1
userText, userRect = self.display_player_name(computerName + " " + "Wins!!!", colors.dark_gray)
elif self.check_if_tie(self.board):
self.gameOver = True
self.scoreboard["ties"] = self.scoreboard.get("ties") + 1
userText, userRect = self.display_player_name("It is a TIE!!!", colors.dark_gray)
self.display_board()
self.screen.blit(userText, userRect)
pygame.display.flip()
Minimax.py
from Connect4 import Connect4
import random
from copy import copy, deepcopy
import pygame
class Minimax(Connect4):
def __init__(self, player1, player2, screen):
super().__init__(player1, player2, screen)
def is_game_over(self, board):
if self.who_won(board, 1) or self.who_won(board, 0):
return True
return False
def generate_move(self, board, depth, computerPlayer, humanPlayer, maximizingPlayer, moveNumber):
if depth == 0 or self.is_game_over(board) or self.check_if_tie(board):
if self.is_game_over(board):
if self.who_won(board, computerPlayer):
return 1000000
elif self.who_won(board, humanPlayer):
return -1000000
elif self.check_if_tie(board):
return 0
else:
return self.get_game_score(board, computerPlayer, humanPlayer)
if maximizingPlayer:
maxValue = -1000000
for move in range(0, self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, computerPlayer, humanPlayer, False, moveNumber + 1)
if result >= maxValue:
maxValue = result
bestMove = move
return bestMove
else:
minValue = 1000000
for move in range(0,self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
if result <= minValue:
minValue = result
thismove = move
return thismove
def copyBoard(self, board):
tmpList = [[self.EMPTY for x in range(self.COLUMNS)] for y in range(self.ROWS)]
for row in range(0, self.ROWS):
for col in range(0, self.COLUMNS):
tmpList[row][col] = board[row][col]
return tmpList
def drop_piece_computer(self, position, board, moveNumber):
"""
Inserting a piece at a given position with the animation of a piece drop
"""
tmpRow = 5
while board[tmpRow][position] == 1 or board[tmpRow][position] == 0:
tmpRow -= 1
board[tmpRow][position] = moveNumber % 2
# moveNumber += 1
def get_game_score(self, board, computerPlayer, humanPlayer):
totalScore = 0
totalScore += self.get_hori_score(board, computerPlayer, humanPlayer)
# totalScore += self.get_vert_score(board, computerPlayer, humanPlayer)
# totalScore += self.get_upright_score(board, computerPlayer, humanPlayer)
# totalScore += self.get_upleft_score(board, computerPlayer, humanPlayer)
return totalScore
def get_hori_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(0, self.COLUMNS - 3):
for row in range(0, self.ROWS):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row][col + 1])
groupingFourList.append(board[row][col + 2])
groupingFourList.append(board[row][col + 3])
computerPieces = self.count_player_pieces(groupingFourList, 1)
humanPieces = self.count_player_pieces(groupingFourList, 0)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def get_upright_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(0, self.COLUMNS - 3):
for row in range(3, self.ROWS):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row - 1][col + 1])
groupingFourList.append(board[row - 2][col + 2])
groupingFourList.append(board[row - 3][col + 3])
computerPieces = self.count_player_pieces(groupingFourList, 1)
humanPieces = self.count_player_pieces(groupingFourList, 0)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def get_upleft_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(3, self.COLUMNS):
for row in range(3, self.ROWS):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row - 1][col - 1])
groupingFourList.append(board[row - 2][col - 2])
groupingFourList.append(board[row - 3][col - 3])
computerPieces = self.count_player_pieces(groupingFourList, 1)
humanPieces = self.count_player_pieces(groupingFourList, humanPlayer)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def get_vert_score(self, board, computerPlayer, humanPlayer):
score = 0
# List to collect all the groupings of 4(Horizontally) out of the current game state
groupingFourList = []
for col in range(0, self.COLUMNS):
for row in range(0, self.ROWS -3):
groupingFourList.append(board[row][col])
groupingFourList.append(board[row + 1][col])
groupingFourList.append(board[row + 2][col])
groupingFourList.append(board[row + 3][col])
computerPieces = self.count_player_pieces(groupingFourList, computerPlayer)
humanPieces = self.count_player_pieces(groupingFourList, humanPlayer)
emptyPieces = self.count_player_pieces(groupingFourList, self.EMPTY)
score += self.score_metric(computerPieces, humanPieces, emptyPieces)
groupingFourList = []
return score
def count_player_pieces(self, groupingFourList, playerPiece):
totalPieces = 0
for piece in groupingFourList:
if piece == playerPiece:
totalPieces += 1
return totalPieces
def score_metric(self, computerPieces, humanPieces, emptyPieces):
score = 0
# Making bot prioritize playing defense than offense
# Thats why the score is lower when regarding the enemy: AI chooses highest scoring move
if (computerPieces == 4):
score += 100
elif (computerPieces == 3 and emptyPieces == 1):
score += 20
elif (computerPieces == 2 and emptyPieces == 2):
score += 10
if (humanPieces == 3 and emptyPieces == 1):
score -= 100
return score
colors.py
"""
Valid colors to use got it from this link : https://python-forum.io/Thread-PyGame-PyGame-Colors
"""
realBlue = (0,0,255)
white = (255,255,255)
green = (0,255,0)
black = (0,0,0)
orange = (255,100,10)
blue_green = (0,255,170)
marroon = (115,0,0)
lime = (180,255,100)
pink = (255,100,180)
purple = (240,0,255)
magenta = (255,0,230)
brown = (100,40,0)
forest_green = (0,50,0)
navy_blue = (0,0,100)
rust = (210,150,75)
dandilion_yellow = (255,200,0)
highlighter = (255,255,100)
sky_blue = (0,255,255)
light_gray = (200,200,200)
dark_gray = (50,50,50)
tan = (230,220,170)
coffee_brown = (200,190,140)
moon_glow = (235, 245, 255)
burlywood = (255, 211, 155)
salmon = (139, 76, 57)
aquamarine = (127, 255, 212)
#Colors used for menu
blue = (135, 206, 250)
yellow = (255, 255, 0)
red = (255,0,0)
gray = (128, 128, 128)
Player.py
class Player():
def __init__(self, name):
self.name = name
The solution is solved but stackoverflow wont allow me to remove the question. The reason I would like the question removed because the answer provided isnt the solution so it would just throw other people off.
Ok the solution is as follows:
In this section of code this is going through a portion of the search space and evaluating the game states based on the best moves for each player. Through this algorithm the AI knows the best moves for each player and can make a "good" move.
if maximizingPlayer:
maxValue = -1000000
for move in range(0, self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, computerPlayer, humanPlayer, False, moveNumber + 1)
if result >= maxValue:
maxValue = result
bestMove = move
return bestMove
else:
minValue = 1000000
for move in range(0,self.COLUMNS):
tmpBoard = self.copyBoard(board)
if self.is_legal_move(move, tmpBoard):
self.drop_piece_computer(move, tmpBoard, moveNumber)
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
if result <= minValue:
minValue = result
thismove = move
return thismove
However, if you look closely when I recursively call the function back to the AI in the !maximizing player function I have:
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
In words when a simulated human player was playing on a generic board to generate the ai move it was assuming that there were 2 human players, and thats why the halt would happen because you cannot have a game with 2 of the same player.
so changing:
result = self.generate_move(tmpBoard, depth - 1, humanPlayer, humanPlayer, True, moveNumber + 1)
to this:
result = self.generate_move(tmpBoard, depth - 1, computerPlayer, humanPlayer, True, moveNumber + 1)
Putting False instead of True in the if maximizingPlayer block and True instead of False in the else clause for the second to last argument to the recursive calls to generate_move seems to fix the game.
i.e. Change False on line 33 of Minimax.py to True and change the True on line 44 to False.

I made a Tic Tac Toe game in python 3 using tkinter, but I don't get any output when I run the code,This is my first time Thank You

import tkinter as tk
window = tk.Tk()
# Code to add widgets will go here
window.title("Tic Tac Toe Board")
window.configure(bg = "white")
theboard = ['']*10
This is the outline of all the widgets
Label1 = tk.Label(window,text ="Player 1 :")
Label1.grid(row=0,column=0)
DisplayMain = tk.Text(master =window,width = 20,height = 2, state = 'disabled')
DisplayMain.grid(row = 0, column = 1)
Entry1 = tk.Entry(master = window)
Entry1.grid(row=1,column=1)
Label2 = tk.Label(window,text ="Player 2 :")
Label2.grid(row=0,column=2)
These are all the functions that I have defined
def buttonclicked(button,board,marker,position):
board[position] = marker
button['text'] = marker
button.configure(state="disabled")
board[position] = marker
user_input = Entry1.get
def executer():
global user_input
user_input = Entry1.get()
I have defined the Buttons here
Button1 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button1,theboard,marker,1))
Button1.grid(row=2,column=0)
Button2 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button2,theboard,marker,2))
Button2.grid(row=2,column=1)
Button3 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button3,theboard,marker,3))
Button3.grid(row=2,column=2)
Button4 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button4,theboard,marker,4))
Button4.grid(row=3,column=0)
Button5 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button5,theboard,marker,5))
Button5.grid(row=3,column=1)
Button6 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button6,theboard,marker,6))
Button6.grid(row=3,column=2)
Button7 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button7,theboard,marker,7))
Button7.grid(row=4,column=0)
Button8 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button8,theboard,marker,8))
Button8.grid(row=4,column=1)
Button9 = tk.Button(window,width = 20,height = 10, text = '', command= lambda: buttonclicked(Button9,theboard,marker,9))
Button9.grid(row=4,column=2)
Button10 = tk.Button(window,width = 20,height=5, text="Click Me", command = lambda: executer())
Button10.grid(row=5,column=1)
These are the rest of the functions
#Function that takes in user input
def player_input():
'''
OUTPUT = (player1marker,player2marker)
'''
global marker
marker = str(user_input)
while marker != 'X' and marker != 'O':
DisplayMain.insert(0.0,'Welcome to Tic-Tac-Toe, Choose X or O to start playing')
player1 = marker
player2 = ''
marker = marker.upper()
if player1 == 'X':
player2 = 'O'
elif player1 == 'O':
player2 = 'X'
else:
pass
continue
return (player1,player2)
#Function that randomly pics who goes first
import random
def choose_first():
player = random.randint(1,2)
if player == 1:
return ("Player 1")
else:
return ("Player 2")
#fucntion which checks if a index position on the board is free returns boolean value
def space_check(board,position):
return board[position] == ''
def full_board_check(board):
for i in range(1,10):
if space_check(board, i):
return False
#return true if full
return True
def win_check(board, mark):
if board[1] == mark and board[2] == mark and board[3]== mark:
return True
elif board[1] == mark and board[4] == mark and board[7] == mark:
return True
elif board[1] == mark and board[5] == mark and board[9] == mark:
return True
elif board[2] == mark and board[5] == mark and board[8]== mark:
return True
elif board[7] == mark and board[5] == mark and board[3]==mark:
return True
elif board[7]== mark and board[8]== mark and board[9] == mark:
return True
elif board[9] == mark and board[6] == mark and board[3] == mark:
return True
elif board[4] == mark and board[5] == mark and board[6] == mark:
return True
else:
return False
def replay():
global DisplayMain
DisplayMain.config(state = 'normal')
DisplayMain.insert(0.0,"Do you want to play again ? ")
play_game = str(user_input)
DisplayMain.config(state = 'disabled')
return play_game.lower()[0] == 'y'
game_on_ready = True
while game_on_ready == True:
player1_marker, player2_marker = player_input()
Label1['text'] = "Player 1 :" + player1_marker
Label2['text'] = "Player 2 :" + player2_marker
#Assigning input of players to variables
DisplayMain.insert(0.0,f"Player 1 chooses {player1_marker}")
DisplayMain.insert(0.0,f"Player 2 chooses {player2_marker}")
DisplayMain.config(state='disabled')
#choosing players turn
turn = choose_first()
DisplayMain.config(state='normal')
DisplayMain.insert(0.0,f'{turn} gets to go first Are you ready to play {turn} ? Enter Yes or No ')
DisplayMain.config(state='disabled')
play_game = str(user_input)
if play_game.lower()[0] == 'y':
game_on_ready = True
else:
game_on_ready = False
This is the working of the game, I think there is a problem with this
section.As, when I run the rest of the code I don't get any errors
while game_on_ready == True :
if turn == 'Player 1':
#Check if won
if win_check(theboard,player1_marker) == True:
DisplayMain.insert(0.0,f"Player 1 ' {player1_marker} ' has won !")
game_on_ready = False
else:
#check if tie
if full_board_check(theboard) == True:
DisplayMain.insert(0.0,"The game is a DRAW")
break
else:
#switch to player 2
turn = "player 2"
else:
#Check if won
if win_check(theboard,player2_marker) == True:
DisplayMain.insert(0.0,f"Player 2 ' {player2_marker} 'has won !")
game_on_ready = False
else:
#check if tie
if full_board_check(theboard) == True:
DisplayMain.insert(0.0,"The game is a DRAW !")
break
else:
#switch to player 1
turn = "Player 1"
if not replay():
DisplayMain.insert(0.0,"Thank you for playing ! HOPE YOU HAD FUN ....")
break
window.mainloop()
#Please review the code and mention the changes that have to be made.

Why doesn't the window update in curses?

I took this nice example of a simple curses application with a list. I wanted to make it scrollable, so I changed the part of the list that gets shown. However, I can scroll down and back up, but the contents shown doesn't change (only the highlighted line, not the lines shown).
What am I doing wrong?
MVCE
#!/usr/bin/env python
import curses
from curses import panel
class Menu(object):
def __init__(self, items, stdscreen):
self.window = stdscreen.subwin(0, 0)
self.window.keypad(1)
self.panel = panel.new_panel(self.window)
self.panel.hide()
panel.update_panels()
self.position = 0
self.items = items
def navigate(self, n):
self.position += n
if self.position < 0:
self.position = 0
elif self.position >= len(self.items):
self.position = len(self.items) - 1
def display(self):
self.panel.top()
self.panel.show()
self.window.clear()
while True:
self.window.refresh()
curses.doupdate()
start = 0
# The next 3 lines seem not to work as intended
while start + (curses.LINES - 1) < self.position:
start += curses.LINES
for index, item in enumerate(self.items[start:curses.LINES - 1], start=start):
if index == self.position:
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL
msg = "%d. %s" % (index, item[0])
self.window.addstr(1 + index, 1, msg, mode)
key = self.window.getch()
if key in [curses.KEY_ENTER, ord("\n"), curses.KEY_RIGHT]:
self.items[self.position][1]()
elif key == curses.KEY_UP:
self.navigate(-1)
elif key == curses.KEY_DOWN:
self.navigate(1)
elif key == curses.KEY_LEFT:
break
self.window.clear()
self.panel.hide()
panel.update_panels()
curses.doupdate()
class MyApp(object):
def __init__(self, stdscreen):
self.screen = stdscreen
curses.curs_set(0)
submenu_items = [("beep", curses.beep), ("flash", curses.flash)]
submenu = Menu(submenu_items, self.screen)
main_menu_items = [
("beep", curses.beep),
("flash", curses.flash),
("submenu", submenu.display),
]
for i in range(200):
main_menu_items.append((f"flash {i}", curses.flash))
main_menu = Menu(main_menu_items, self.screen)
main_menu.display()
if __name__ == "__main__":
curses.wrapper(MyApp)
Basically that's because you're not updating the upper limit on the slice used in this loop:
for index, item in enumerate(self.items[start:curses.LINES - 1], start=start):
Here's a better version
MVCE
#!/usr/bin/env python
import curses
from curses import panel
class Menu(object):
def __init__(self, items, stdscreen):
self.window = stdscreen.subwin(0, 0)
self.window.keypad(1)
self.panel = panel.new_panel(self.window)
self.panel.hide()
panel.update_panels()
self.position = 0
self.items = items
def navigate(self, n):
self.position += n
if self.position < 0:
self.position = 0
elif self.position >= len(self.items):
self.position = len(self.items) - 1
def display(self):
self.panel.top()
self.panel.show()
self.window.clear()
while True:
start = 0
self.window.clear()
while start + (curses.LINES - 1) < self.position:
start += curses.LINES
myrow = self.position - start
mycol = 0
for index, item in enumerate(self.items[start:start + curses.LINES - 1], start=start):
if index == self.position:
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL
msg = "%d. %s" % (index, item[0])
self.window.addstr(index - start, 0, msg, mode)
if index == self.position:
(myrow, mycol) = self.window.getyx()
self.window.move(myrow, mycol)
key = self.window.getch()
if key in [curses.KEY_ENTER, ord("\n"), curses.KEY_RIGHT]:
self.items[self.position][1]()
elif key == curses.KEY_UP:
self.navigate(-1)
elif key == curses.KEY_DOWN:
self.navigate(1)
elif key == curses.KEY_LEFT:
break
self.window.clear()
self.panel.hide()
panel.update_panels()
curses.doupdate()
class MyApp(object):
def __init__(self, stdscreen):
self.screen = stdscreen
curses.curs_set(1)
submenu_items = [("beep", curses.beep), ("flash", curses.flash)]
submenu = Menu(submenu_items, self.screen)
main_menu_items = [
("beep", curses.beep),
("flash", curses.flash),
("submenu", submenu.display),
]
for i in range(200):
main_menu_items.append((f"flash {i}", curses.flash))
main_menu = Menu(main_menu_items, self.screen)
main_menu.display()
if __name__ == "__main__":
curses.wrapper(MyApp)

Why does my program stop responding and crashing?

I was just trying to learn some PyGame which will be needed for my coming project so I would like to know why this sample piece of code is crashing and how I can fix it.
I've tried looking online and haven't found a working solution.
import pygame
# Initialise pygame
pygame.init()
clock = pygame.time.Clock()
# pygame.font.init()
myFont = pygame.font.SysFont('Comic Sans MS', 30)
# Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Screen Size
size = [400, 300]
screen = pygame.display.set_mode(size)
# Initialise Variables
done = False
class Unit:
def __init__(self, name, hp, attack):
self.name = name
self.hp = hp
self.attack = attack
class Infantry(Unit):
def __init__(self, name, hp, attack):
super().__init__(name, hp, attack)
def attack_turn(self):
self.hp -= self.attack
text_surface = myFont.render(self.name + " has " + str(self.hp) + " health points left!", True, (255, 0, 0))
screen.blit(text_surface, (0, 0))
# print(self.name + " has " + str(self.hp) + " health points left!")
x = input("Press ENTER to continue!")
player = Infantry("Panzer", 110, 13) # The attack set here is for the enemy.
enemy = Infantry("T-14", 100, 30) # The attack set here is for the player.
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
while player.hp > 0 and enemy.hp > 0:
player.attack_turn()
enemy.attack_turn()
done = True
if player.hp > enemy.hp:
# print(player.name + " wins!")
text_surface = myFont.render(player.name + " wins!", False, (255, 0, 0))
screen.blit(text_surface, (100, 100))
else:
# print(enemy.name + " wins!")
text_surface = myFont.render(enemy.name + " wins!", False, (255, 0, 0))
screen.blit(text_surface, (100, 100))
I hoped that the code would at least run so I could work off there but it's not even doing that.
Don't do another loop inside the main loop. The inner loop prevents the event handling, get rid of it. It is sufficient to check if the game has finished at the end of the main loop:
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
player.attack_turn()
enemy.attack_turn()
if player.hp <= 0 or enemy.hp <= 0:
done = True
pygame.display.flip()
input does not do what you want it is not an input to the game window. Delete it and sperate the attack method to on method which calculates the new health and a ne method draw, which "blit" the text:
class Infantry(Unit):
def __init__(self, name, hp, attack):
super().__init__(name, hp, attack)
def attack_turn(self):
self.hp -= self.attack
### x = input("Press ENTER to continue!") <----- delete
def draw(self):
text_surface = myFont.render(self.name + " has " + str(self.hp) + " health points left!", True, (255, 0, 0))
screen.blit(text_surface, (0, 0))
Use the pygame.KEYUP event to check if the pygame.K_RETURN key was pressed.
Further do the all the drawing at the end of the main loop. Use pygame.Surface.fill to fill screen by a single color (clear the screen). Then draw the text. Finally use pygame.display.flip() to update the full display:
e.g.
done = False
count = 0
while not done:
attack = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
attack = True
if attack:
if count % 2 == 0:
player.attack_turn()
else:
enemy.attack_turn()
count += 1
if player.hp <= 0 or enemy.hp <= 0:
done = True
screen.fill(BLACK)
if count % 2 == 0:
player.draw()
else:
enemy.draw()
pygame.display.flip()

What is wrong with my elif statement? Invalid Syntax

if option == "1":
with open("sample.txt","r") as f:
print(f.read())
numbers = []
with open("sample2.txt","r") as f:
for i in range(9):
numbers.append(f.readline().strip())
print(numbers)
from random import randint
for i in range(9):
print(numbers[randint(0,8)])
from tkinter import *
def mhello():
mtext = ment.get()
mLabel2 = Label(test, text=mtext).pack()
return
test = Tk()
ment = StringVar()
test.geometry('450x450+500+10')
test.title('Test')
mlabel = Label(test, text='Time to guess').pack()
mbutton = Button(test, text='Click', command = mhello).pack()
mEntry = Entry(test, textvariable=ment).pack()
test.mainloop()
from tkinter import *
def mhello():
my_word = 'HELLO'
mtext = ment.get()
if my_word == mtext:
mLabel2 = Label(test, text='Correct').pack()
else:
mLabel2 = Label(test, text='Incorrect').pack()
return
test = Tk()
ment = StringVar()
test.geometry('450x450+500+300')
test.title('Test')
def label_1():
label_1 = Label(test, text='Hello. Welcome to my game.').pack()
def label_2():
label_2 = Label(test, text='What word am I thinking of?').pack()
button_1 = Button(test, text='Click', command = mhello).pack()
entry_1 = Entry(test, textvariable=ment).pack()
label_1()
test.after(5000, label_2)
test.mainloop()
from tkinter import *
from random import shuffle
game = Tk()
game.geometry('200x200')
game.grid()
game.title("My Game")
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def board_1():
board1 = []
k = 0
for i in range(3):
for j in range(3):
board1.append(Label(game, text = board[k]))
board1[k].grid(row = i, column = j)
k +=1
def board_2():
shuffle(board)
board2 = []
k = 0
for i in range(3):
for j in range(3):
board2.append(Label(game, text = board[k]))
board2[k].grid(row = i, column = j)
k +=1
board_1()
game.after(5000, board_2)
game.mainloop()
#2nd Option
elif option == "2":
print ("You have chosen option 2. Well Done, You Can Type! XD")
The bit that has the Syntax Error is the 1st elif statement (2nd Option). Ignore all the code prior to this if necessary (it is there for context). Whenever I run the code it says that there is a syntax error and just positions the typing line (I don't know what it's called) at the end of the word elif.
This is a simple fix, with if else statements you need to have a closing ELSE and in this case there is not so when your program runs it sees that theres a lonely if without its else :)
if option == "1":
elif option == "2":
else:
'do something else in the program if any other value was recieved'
also a switch statement can be used here so it does not keep checking each condition and just goes straight to the correct case :D
The problem is that your block is separated from the first if-statement, where it actually belongs to. As it is, it follows the game.mainloop() statement, and adds an unexpected indentation. Try to rearrange your code like so:
if option == "1":
with open("sample.txt","r") as f:
print(f.read())
numbers = []
with open("sample2.txt","r") as f:
for i in range(9):
numbers.append(f.readline().strip())
print(numbers)
from random import randint
for i in range(9):
print(numbers[randint(0,8)])
elif option == "2":
print ("You have chosen option 2. Well Done, You Can Type! XD")
[ Rest of the code ]

Resources