Advertisement
AlfonsoPEREZ

Connect 4

May 2nd, 2020
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. import turtle
  2.  
  3. Pen = turtle.Turtle()
  4. Pen.hideturtle()
  5. Pen.speed(500)
  6. window = turtle.Screen()
  7. window.screensize(1000, 400)
  8. window.bgcolor("powderblue")
  9.  
  10. Pen.speed(0)
  11. Pen._tracer(8, 25)
  12. turtle.setup(1000, 1000)
  13.  
  14. connect4 = [[0 for c in range(7)] for x in range(6)]
  15.  
  16.  
  17. def draw_grid(grid):
  18.     Pen.setheading(0)
  19.     Pen.goto(-350, 130)
  20.     for rower in range(0, 6):
  21.         for col in range(0, 7):
  22.             if grid[rower][col] == 0:
  23.                 Pen.fillcolor("white")
  24.             elif grid[rower][col] == 2:
  25.                 Pen.fillcolor("red")
  26.             elif grid[rower][col] == 1:
  27.                 Pen.fillcolor("yellow")
  28.  
  29.             Pen.begin_fill()
  30.             Pen.circle(25)
  31.             Pen.end_fill()
  32.  
  33.             Pen.penup()
  34.             Pen.forward(58)
  35.             Pen.pendown()
  36.         Pen.setheading(270)
  37.         Pen.penup()
  38.         Pen.forward(58)
  39.         Pen.setheading(180)
  40.         Pen.forward(58 * 7)
  41.         Pen.setheading(0)
  42.         Pen.getscreen().update()
  43.  
  44.  
  45. def draw_board():
  46.     Pen.up()
  47.     Pen.setheading(0)
  48.     Pen.goto(-386, -200)
  49.     Pen.begin_fill()
  50.     for b in range(4):
  51.         Pen.color("blue")
  52.         Pen.pendown()
  53.         Pen.forward(420)
  54.         Pen.left(90)
  55.     Pen.up()
  56.     Pen.end_fill()
  57.  
  58.  
  59. def draw_game_panel():
  60.     Pen.up()
  61.     Pen.setheading(0)
  62.     Pen.goto(80, 219)
  63.     Pen.begin_fill()
  64.     for rectangle in range(2):
  65.         Pen.color("white")
  66.         Pen.down()
  67.         Pen.forward(250)
  68.         Pen.right(90)
  69.         Pen.forward(418)
  70.         Pen.right(90)
  71.     Pen.end_fill()
  72.     Pen.up()
  73.     Pen.color("black")
  74.     Pen.goto(-150, 250)
  75.     Pen.write("CONNECT 4", True, align="center", font=("Arial", 40, "bold"))
  76.  
  77.  
  78. def check_if_winner(grid, color):
  79.     # Vertical row checking
  80.     for r in range(6):
  81.         for c in range(4):
  82.             if grid[r][c] == color and grid[r][c+1] == color and grid[r][c+2] == color and grid[r][c+3] == color:
  83.                 return color
  84.     # Horizontal row checking
  85.     for x in range(3):
  86.         for y in range(7):
  87.             if grid[x][y] == color and grid[x+1][y] == color and grid[x+2][y] == color and grid[x+3][y] == color:
  88.                 return color
  89.     # Diagonal checking
  90.     for i in range(3):
  91.         for z in range(4):
  92.             if grid[i][z] == color and grid[i+1][z+1] == color and grid[i+2][z+2] == color and grid[i+3][z+3] == color:
  93.                 return color
  94.     # Diagonal checking
  95.     for d in range(5, 2, -1):
  96.         for c in range(4):
  97.             if grid[d][c] == color and grid[d-1][c+1] == color and grid[d-2][c+2] == color and grid[d-3][c+3] == color:
  98.                 return color
  99.  
  100.     return 0
  101.  
  102.  
  103. def display_winner(winners):
  104.     if winners == 2:
  105.         Pen.penup()
  106.         Pen.color("red")
  107.         Pen.goto(200, 150)
  108.         Pen.write("RED WINS", True, align="center", font=("Arial", 20, "bold"))
  109.         Pen.getscreen().update()
  110.         return True
  111.     elif winners == 1:
  112.         Pen.penup()
  113.         Pen.color("yellow")
  114.         Pen.goto(200, 150)
  115.         Pen.write("YELLOW WINS", True, align="center", font=("Arial", 20, "bold"))
  116.         Pen.getscreen().update()
  117.         return True
  118.  
  119.  
  120. def loop():
  121.     # Game loop, play up to 42 turns
  122.     for player_turn in range(1, 43):
  123.         column_string = window.numinput("Your turn", "Pick column number:", 1, minval=1, maxval=7)
  124.         column = int(column_string)
  125.         column_minus = column - 1
  126.         while connect4[0][column_minus] != 0:
  127.             # This column is already full, pick another one
  128.             column_string = window.numinput("Your turn", "Pick other column number row is full:", 1, minval=1, maxval=7)
  129.             column = int(column_string)
  130.             column_minus = column - 1
  131.  
  132.         # Make the chips stack up one another
  133.         row = 5
  134.         while connect4[row][column_minus] != 0:
  135.             row = row - 1
  136.         # Find out the colour of the current player (1 or 2)
  137.         playerColor = int((player_turn % 2) + 1)
  138.         # Place the token on the grid
  139.         connect4[row][column_minus] = playerColor
  140.         # Draw the grid
  141.         winner = check_if_winner(connect4, playerColor)
  142.         draw_grid(connect4)
  143.         if display_winner(winner):
  144.             user_input = window.textinput("Exit", "Type 'quit' to exit the game")
  145.             while True:
  146.                 if user_input == 'quit':
  147.                     print("Game has been exited!")
  148.                     break
  149.                 else:
  150.                     user_input = window.textinput("Exit", "Type 'quit' to exit the game")
  151.             break
  152.         draw_grid(connect4)
  153.  
  154.  
  155. draw_game_panel()
  156. draw_board()
  157. draw_grid(connect4)
  158. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement