puissance4.py

Created by loic-azavant

Created on September 20, 2023

3.82 KB

fonctionne avec upsilon


from kandinsky import *
from ion import *
from time import *



class Connect4:
  
  def __init__(self):
    self.grid = [[0 for i in range(7)] for j in range(6)]
    self.players_nb_pieces = [21,21]
    self.selected_col = 2
    self.current_player = 0
    self.possible_col = [True for i in range(7)]
  
  def drw_piece(self,x,y,c='w'):
    fill_circle(x*30+70,y*30+52,10,c)
  
  def drw_grid(self):
    fill_rect(55,37,210,185,'b')
    fill_rect(54,38,212,184,'b')
    for i in range(6):
      for j in range(7):
        self.drw_piece(j,i)
  
  def drw_pieces(self):
    fill_rect(5,0,20,222-5*self.players_nb_pieces[0],'w')
    fill_rect(5,222-5*self.players_nb_pieces[0],20,5*self.players_nb_pieces[0],'r')
    fill_rect(295,0,20,222-5*self.players_nb_pieces[1],'w')
    fill_rect(295,222-5*self.players_nb_pieces[1],20,5*self.players_nb_pieces[1],'y')
  
  def drw_selected_col(self):
    fill_rect(65,10,190,18,'w')
    draw_string('V',min(self.selected_col*30+65,245),10)
  
  def add_piece_to_col(self):
    fill_circle(self.selected_col*30+70,self.lowest_pos()*30+52,10,(255,255*self.current_player,0))
    y = self.lowest_pos()
    self.grid[self.lowest_pos()][self.selected_col] = self.current_player+1
    self.players_nb_pieces[self.current_player] -= 1
    self.check_current_col()
    self.next_turn()
    self.check_win(y,self.selected_col,self.current_player+1)
   
  def check_win(self,y,x,p):
    if self.players_nb_pieces[self.current_player] == 0:
      draw_string('Draw !',130,110)
      while 1: pass
    self.check_hori()
    self.check_verti()
    self.check_diagonals()
  
  def check_hori(self):
    for i in range(6):
      for j in range(4):
        if self.grid[i][j] == self.grid[i][j+1] == self.grid[i][j+2] == self.grid[i][j+3] and self.grid[i][j] != 0:
          self.win(self.grid[i][j])
  
  def check_verti(self):
    for i in range(7):
      for j in range(3):
        if self.grid[j][i] == self.grid[j+1][i] == self.grid[j+2][i] == self.grid[j+3][i] and self.grid[j][i] != 0:
          self.win(self.grid[j][i])
  
  def check_diagonals(self):
    for i in range(6):
      for j in range(round(-abs(2.5-i)+3.5)):
        x = (0 if i < 3 else i-2)+j
        y = max(2-i,0)+j
        if self.grid[y][x] == self.grid[y+1][x+1] == self.grid[y+2][x+2] == self.grid[y+3][x+3] and self.grid[y][x] != 0:
          self.win(self.grid[y][x])
    for i in range(6):
      for j in range(round(-abs(2.5-i)+3.5)):
        x = (0 if i < 3 else i-2)+j
        y = min(i+3,5)-j
        if self.grid[y][x] == self.grid[y-1][x+1] == self.grid[y-2][x+2] == self.grid[y-3][x+3] and self.grid[y][x] != 0:
          self.win(self.grid[y][x])
  
  def win(self,p):
    draw_string("".join(['Player ',str(p),' win']),100,110)
    while 1: pass

  def lowest_pos(self):
    for i in range(6):
      if self.grid[5-i][self.selected_col] == 0:
        return 5-i
    return -1
  
  def check_current_col(self):
    if self.lowest_pos() == -1:
      self.possible_col[self.selected_col] = False
      self.selected_col = 0
      for i in range(7):
        if not self.possible_col[self.selected_col]:
          self.selected_col += 1
        else:
          break

  def next_turn(self):
    self.current_player = (self.current_player+1)%2
    self.drw_pieces()
    self.drw_selected_col()
  
  def change_selected_col(self,s):
    try:
      for i in range(1,7):
        if self.possible_col[self.selected_col+s*i] and self.selected_col+s*i >= 0:
          self.selected_col += s*i
          break
    except: pass

  def play(self):
    while 1:
      if keydown(4):
        a.add_piece_to_col()
        while keydown(4): pass
      b = keydown(3)-keydown(0)
      if b:
        self.change_selected_col(b)
        self.drw_selected_col()
        while keydown(3) or keydown(0): pass

a=Connect4()
a.drw_grid()
a.drw_pieces()
a.drw_selected_col()
a.check_diagonals()
a.play()

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.