SoFunction
Updated on 2024-11-17

Python implementation of backgammon game

In this article, we share examples of python to achieve the specific code of the game of backgammon, for your reference, the details are as follows

Without further ado, let's get right to the code:

Full project files at GitHub:five-in-a-row (game similar tic-tac-toe)

Effect Preview:

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
import pygame
from  import *
from sys import exit
import numpy
background_image = ''
white_image = ''
black_image = ''
 
def WhoWin(x,y,darray):
 num1,num2,num3,num4 = 0,0,0,0
 #Judge up, down, left, right, left up, left up, left down, right down, 8 directions.
 i = x-1
 while(i>=0):
 if darray[i][y] == 1:
  num1+=1
  i -= 1
 else:
  break
 i = x+1
 while i<19:
 if darray[i][y] == 1:
  num1+=1
  i += 1
 else:
  break
 j =y-1
 while (j >= 0):
 if darray[x][j] == 1:
  num2 += 1
  j -= 1
 else:
  break
 j = y + 1
 while j < 19:
 if darray[x][j] == 1:
  num2 += 1
  j += 1
 else:
  break
 
 i,j = x-1,y-1
 while(i>=0 and j>=0):
 if darray[i][j] == 1:
  num3 += 1
  i -= 1
  j -= 1
 else :
  break
 i, j = x + 1, y + 1
 while (i < 19 and j < 19):
 if darray[i][j] == 1:
  num3 += 1
  i += 1
  j += 1
 else:
  break
 
 i, j = x + 1, y - 1
 while (i >= 0 and j >= 0):
 if darray[i][j] == 1:
  num4 += 1
  i += 1
  j -= 1
 else:
  break
 i, j = x - 1, y + 1
 while (i < 19 and j < 19):
 if darray[i][j] == 1:
  num4 += 1
  i -= 1
  j += 1
 else:
  break
 
#Five wins
 if num1>=4 or num2>=4 or num3 >= 4 or num4 >= 4:
 return True
 else:
 return False
 
# Initialization
()
#Screen, #Background image, #White-Black sub conversion
screen = .set_mode((584, 584), RESIZABLE, 32)
background = (background_image).convert()
white = (white_image).convert_alpha()
black = (black_image).convert_alpha()
#title drawing fonts
(background, (0,0))
font = ("arial", 40);
.set_caption('Pentominoes')
 
#zeros() returns an array of 19 rows and 19 columns.
white_luodian = ((19,19))
black_luodian = ((19,19))
 
# Set the coordinates of all points of the board
qipan_list = [(30+i*29-12,30+j*29-12) for i in range(19) for j in range(19)]
#Defaulting to black's first move, switching moves.
transW_B = True
# Game main loop
while True:
 
 for event in ():
 if  == QUIT:
  exit()
 if  == MOUSEBUTTONDOWN:
  x,y = .get_pos()
  if 30 <= x <= 554 and 30 <= y <= 554 and ((x - 30) % 29 <= 12 or (x - 30) % 29 >= 17) and (
   (y - 30) % 29 <= 12 or (y - 30) % 29 >= 17):
  # Rounding up
  m = int(round((x-30)/29))
  n = int(round((y-30)/29))
  # Results analysis
  if transW_B:
   transW_B = not transW_B
   (black, qipan_list[19*m+n])
   black_luodian[n][m] = 1
   if WhoWin(n,m,black_luodian):
   (('Black chess player wins!', True, (0, 0, 0),(0,229,238)), (120, 280))
 
  else:
   transW_B = not transW_B
   (white, qipan_list[19 * m + n])
   white_luodian[n][m] = 1
   if WhoWin(n,m,white_luodian):
   (('White chess player wins!', True, (255, 255, 255),(0,229,238)), (120, 280))
 
  qipan_list[19*m+n] = ''
 
 ()

This is the whole content of this article.