[text] connect 4

Viewer

copydownloadembedprintName: connect 4
  1. from pygame import*
  2. #init
  3. init()
  4.  
  5. grid = [[' ',' ',' ',' ',' ',' ',' '],
  6.                 [' ',' ',' ',' ',' ',' ',' '],
  7.                 [' ',' ',' ',' ',' ',' ',' '],
  8.                 [' ',' ',' ',' ',' ',' ',' '],
  9.                 [' ',' ',' ',' ',' ',' ',' '],
  10.                 [' ',' ',' ',' ',' ',' ',' ']]
  11.  
  12. #size of the screen and create the screen
  13. width = 800
  14. height = 700
  15. screen = display.set_mode((width,height))
  16. endProgram = False
  17. win = ' '
  18. counter = 'R'
  19.  
  20. def swapCounter(counter):
  21.         if counter == 'R':
  22.                 counter = 'Y'
  23.         else:
  24.                 counter = 'R'
  25.         return counter
  26.  
  27. def updateGrid(grid,b,counter):
  28.         a = 5
  29.         while a >= 0:
  30.                 if grid[a][b] == ' ':
  31.                         grid[a][b] = counter
  32.                         a = -1
  33.                 if grid[a][b] != ' ':
  34.                         a = a - 1
  35.         return grid
  36.        
  37. def checkDiags1(Max,row,col):
  38.         count = 0
  39.         while col <= 5 and row <= 6:
  40.                 if grid[col][row] == counter:
  41.                         count = count + 1
  42.                         if Max < count:
  43.                                 Max = count
  44.                 else:
  45.                         count = 0
  46.                 row = row + 1
  47.                 col = col + 1
  48.         return Max
  49.  
  50. def checkDiags2(Max,row,col):
  51.         count = 0
  52.         while col >= 0 and row <= 5:
  53.                 if grid[col][row] == counter:
  54.                         count = count + 1
  55.                         if Max < count:
  56.                                 Max = count
  57.                 else:
  58.                         count = 0
  59.                 row = row + 1
  60.                 col = col - 1
  61.         return Max
  62.  
  63. def checkWin(counter):
  64.         row = 0
  65.         count = 0
  66.         Max = 0
  67.         while row <= 6:
  68.                 col = 0
  69.                 while col <= 5:
  70.                         if grid[col][row] == counter:
  71.                                 count = count + 1
  72.                                 if count > Max:
  73.                                         Max = count
  74.                         else:
  75.                                 count = 0
  76.                         col = col + 1
  77.                 row = row + 1
  78.        
  79.         col = 0
  80.         count = 0
  81.         while col <= 5:
  82.                 row = 0
  83.                 while row <= 6:
  84.                         if grid[col][row] == counter:
  85.                                 count = count + 1
  86.                                 if count > Max:
  87.                                         Max = count
  88.                         else:
  89.                                 count = 0
  90.                         row = row + 1
  91.                 col = col + 1
  92.                
  93.         Max = checkDiags1(Max,2,0)
  94.         Max = checkDiags1(Max,1,0)
  95.         Max = checkDiags1(Max,0,0)
  96.         Max = checkDiags1(Max,0,1)
  97.         Max = checkDiags1(Max,0,2)
  98.         Max = checkDiags1(Max,0,3)
  99.        
  100.         Max = checkDiags2(Max,2,5)
  101.         Max = checkDiags2(Max,1,5)
  102.         Max = checkDiags2(Max,0,5)
  103.         Max = checkDiags2(Max,0,4)
  104.         Max = checkDiags2(Max,0,3)
  105.         Max = checkDiags2(Max,0,2)
  106.        
  107.         print(Max,counter)
  108.         if Max >= 4:
  109.                 print('WIN')
  110.                 return counter
  111.  
  112.  
  113. #game loop - always needed. all none set up
  114. #stuff goes in here
  115. while endProgram == False:
  116.         for e in event.get():
  117.                 if e.type == QUIT:
  118.                         endProgram = True
  119.                 if e.type == MOUSEBUTTONDOWN:
  120.                         X,Y = mouse.get_pos()
  121.                         if X >= 60 and X <= 140:
  122.                                 b = 0
  123.                                 grid = updateGrid(grid,b,counter)
  124.                         if X >= 160  and X <= 240:
  125.                                 b = 1
  126.                                 grid = updateGrid(grid,b,counter)
  127.                         if X >= 260  and X <= 340:
  128.                                 b = 2
  129.                                 grid = updateGrid(grid,b,counter)
  130.                         if X >= 360  and X <= 440:
  131.                                 b = 3
  132.                                 grid = updateGrid(grid,b,counter)
  133.                         if X >= 460  and X <= 540:
  134.                                 b = 4
  135.                                 grid = updateGrid(grid,b,counter)
  136.                         if X >= 560  and X <= 640:
  137.                                 b = 5
  138.                                 grid = updateGrid(grid,b,counter)
  139.                         if X >= 660  and X <= 740:
  140.                                 b = 6
  141.                                 grid = updateGrid(grid,b,counter)
  142.                         win = checkWin(counter)
  143.                         counter = swapCounter(counter)
  144.                         print (win)
  145.  
  146.  
  147.         screen.fill((0,0,0))
  148.         #screen is where we will be drawing to
  149.         #(31,182,255) is the colour of the rectangle
  150.         #(50,50,700,500) is the co-ordinates of the rectangle
  151.         #x,y, width, height
  152.         draw.rect(screen,(31,182,255),(50,50,700,600))
  153.         x = 100
  154.         y = 100
  155.         r = 40
  156.         col = 0
  157.         row = 0
  158.         while col <= 5:
  159.                         row = 0
  160.                         x = 100
  161.                         while row <= 6:
  162.                                 if grid[col][row] == ' ':
  163.                                         draw.circle(screen,(255,255,255), (x,y),r)
  164.                                 elif grid[col][row] == 'R':
  165.                                         draw.circle(screen,(255,0,0), (x,y),r)
  166.                                 elif grid[col][row] == 'Y':
  167.                                         draw.circle(screen,(250,220,22), (x,y),r)
  168.                                 row = row + 1
  169.                                 x = x + 100
  170.                         col = col + 1
  171.                         y = y + 100
  172.                
  173.         if win == 'R':
  174.                 draw.rect(screen,(255,0,0),(50,50,700,600))
  175.         if win == 'Y':
  176.                 draw.rect(screen,(250,220,22),(50,50,700,600))
  177.         #show the newly drawn screen
  178.         display.flip()
  179.         #short delay to show animation
  180.         time.delay(5)
  181.  

Editor

You can edit this paste and save as new:


File Description
  • connect 4
  • Paste Code
  • 22 Oct-2020
  • 3.76 Kb
You can Share it: