test.py

Created by naul

Created on April 01, 2023

2.63 KB


from math import *
from cmath import sqrt
import matplotlib.pyplot as plt
import turtle
import random
import kandinsky as kd
import time

# Connect Four game functions

def create_board(rows, cols):
    return [[0 for _ in range(cols)] for _ in range(rows)]

def make_move(board, col, player):
    row = len(board) - 1
    while board[row][col] != 0:
        row -= 1
    board[row][col] = player

def is_winner(board, player):
    # Check rows
    for row in board:
        if [player]*4 in [row[i:i+4] for i in range(len(row)-3)]:
            return True
    # Check columns
    for j in range(len(board[0])):
        if [player]*4 in [[board[i][j], board[i+1][j], board[i+2][j], board[i+3][j]] for i in range(len(board)-3)]:
            return True
    # Check diagonals
    for i in range(len(board)-3):
        for j in range(len(board[0])-3):
            if board[i][j] == player and board[i+1][j+1] == player and board[i+2][j+2] == player and board[i+3][j+3] == player:
                return True
    for i in range(len(board)-3):
        for j in range(3, len(board[0])):
            if board[i][j] == player and board[i+1][j-1] == player and board[i+2][j-2] == player and board[i+3][j-3] == player:
                return True
    return False

def ai_algorithm(board):
    return random.choice([col for col in range(len(board[0])) if board[0][col] == 0])

# Graphics functions

def init():
    kd.fill_rect(0, 0, kd.get_screen_width(), kd.get_screen_height(), (255, 255, 255))

def close():
    pass

def display_board(board):
    kd.clear()
    kd.draw_string('Connect Four', 10, 10)
    kd.draw_string('Generation: {}'.format(generation), 10, 40)
    for row in range(len(board)):
        for col in range(len(board[0])):
            if board[row][col] == 0:
                kd.fill_rect(col*40+20, row*40+60, 40, 40, (255, 255, 255))
            elif board[row][col] == 1:
                kd.fill_circle(col*40+40, row*40+80, 16, (255, 0, 0))
            else:
                kd.fill_circle(col*40+40, row*40+80, 16, (0, 0, 255))
    kd.show()

def key_down(key):
    return False

def set_auto_refresh(enable):
    pass

# Main loop

def main():
    init()
    set_auto_refresh(False)
    generation = 1
    while not key_down(KEY_OK):
        display_board(board)
        col = ai_algorithm(board)
        make_move(board, col, 1)
        if is_winner(board, 1):
            print("AI wins!")
            break
        col = ai_algorithm(board)
        make_move(board, col, 2)
        if is_winner(board, 2):
            print("Player wins!")
            break
        generation += 1
        time.sleep(0.5)
    close()

if __name__ == '__main__':
    main()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.