Connecting The Dots

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Connect The Dots

Guided by
Manjusha Gaikwad

Department of Computer Engineering and


Information Technology

1
Members:
1. Aditya Raina 1710080002
2. Charudatt Mahajan 171080021
3. Govind Tadvi 171080009

2
Table of Contents:
Sr.No. Name P.N
1 Abstract 4
2 Requirements 5
3 Introduction 9
4 Strategy 10
5 Code 16
6 Conclusion 21
7 References 21

3
Abstract :
Connect Four (also known as Captain's Mistress, Four Up, Plot Four, Find Four, Four in
a Row, Four in a Line, Drop Four, and Gravitrips (in Soviet Union)) is a two-
player connection game in which the players first choose a color and then take turns dropping
one colored disc from the top into a seven-column, six-row vertically suspended grid. The
pieces fall straight down, occupying the lowest available space within the column. The
objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of
one's own discs. Connect Four is a solved game. The first player can always win by playing
the right moves.
The game was first sold under the Connect Four trademark by Milton Bradley in February
1974.

Gameplay:
Objective: Connect four of your checkers in a row while preventing your opponent from doing the same.
But, look out -- your opponent can sneak up on you and win the game!

4
Requirements :
 Python latest version installed
 Basic python knowledge
 Pygame installed
 Numpy installed
 Basic Knowledge on Command prompt

5
Pygame : Pygame (the library) is a Free and Open Source python programming language
library for making multimedia applications like games built on top of the excellent SDL library.
Like SDL, pygame is highly portable and runs on nearly every platform and operating system.
Pygame Features :

 Silliness built in. Pygame is meant to make software things fun. New silliness is added
every 3.1415 seconds.

 Does not require OpenGL. With many people having broken OpenGL setups,
requiring OpenGL exclusively will cut into your user base significantly. Pygame uses
either opengl, directx, windib, X11, linux frame buffer, and many other different
backends... including an ASCII art backend! OpenGL is often broken on linux systems,
and also on windows systems - which is why professional games use multiple backends.

 Multi core CPUs can be used easily. With dual core CPUs common, and 8 core CPUs
cheaply available on desktop systems, making use of multi core CPUs allows you to do
more in your game. Selected pygame functions release the dreaded python GIL, which
is something you can do from C code.

 Uses optimized C, and Assembly code for core functions. C code is often 10-20 times
faster than python code, and assembly code can easily be 100x or more times faster
than python code.

 Comes with many Operating systems. Just an apt-get, emerge, pkg_add, or yast
install away. No need to mess with installing it outside of your operating systems
package manager. Comes with binary installers (and uninstallers) for Windows or
MacOS X.

 Truly portable. Supports Linux (pygame comes with most mainstream linux
distributions), Windows (95,98,me,2000,XP,vista, 64bit windows etc), Windows CE,
BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX,
and QNX. The code contains support for AmigaOS, Dreamcast, Atari, AIX,
OSF/Tru64, RISC OS, SymbianOS, and OS/2, but these are not officially supported.
You can use it on hand held devices from nokia, game consoles like gp2x, the One
Laptop Per Child (OLPC), and the Orange Pi.

 It's Simple and easy to use. Kids and adults make games with pygame. Before the
Raspberry Pi, the microbit, or the OLPC, Pygame has been taught in courses to young
kids, and college students. It's also used by people who first programmed in z80
assembler, or c64 basic, and for Indie game productions.

 Does not require a GUI to use all functions. You can use pygame without a monitor
- like if you want to use it just to process images, get joystick input, or play sounds.

 Small amount of code. It does not have hundreds of thousands of lines of code for
things you won't use anyway. The core is kept simple, and extra things like GUI
libraries, and effects are developed separately outside of pygame.

6
Pygame Installation : Pygame requires Python; if you don't already have it, you can download
it from python.org. Use python 3.6.1 or greater, because it is much friendlier to newbies, and
additionally runs faster.
The best way to install pygame is with the pip tool (which is what python uses to install
packages). Note, this comes with python in recent versions. We use the --user flag to tell it to
install into the home directory, rather than globally.

python3 -m pip install -U pygame --user

Numpy : NumPy is the fundamental package for scientific computing with Python. It contains
among other things:

 a powerful N-dimensional array object


 sophisticated (broadcasting) functions
 tools for integrating C/C++ and Fortran code
 useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional
container of generic data. Arbitrary data-types can be defined. This allows NumPy to
seamlessly and speedily integrate with a wide variety of databases.
NumPy is licensed under the BSD license, enabling reuse with few restrictions.

Numpy Installation :
python3 -m pip install -U numpy --user

7
List of Figures :

1. Game starting window


2. Gameplay
3. Player 1 wins
4. Player 2 wins

8
Introduction:

The game is a modification to the traditional Connect Four game. It is a two-player game with
"perfect information". This term describes games where one player at a time plays, players
have all the information about moves that have taken place, and all moves that can take place,
for a given game state. It also belongs to the classification of an adversarial, zero-sum game,
since a player's advantage is an opponent's disadvantage.

One measure of complexity of the game is the number of possible games board positions. For
classic Connect Four played on 6 high, 7 wide grid, there are 4,531,985,219,092 positions for
all game boards populated with 0 to 42 pieces.

The game was first solved by James Dow Allen (October 1, 1988), and independently
by Victor Allis (October 16, 1988). Allis describes a knowledge-based approach, with nine
strategies, as a solution for Connect Four. Allen also describes winning strategies in his
analysis of the game. At the time of the initial solutions for Connect Four, brute-force analysis
was not deemed feasible given the game's complexity and the computer technology available
at the time.

Connect Four has since been solved with brute force methods beginning with John Tromp's
work in compiling an 8-ply database (February 4, 1995). The artificial intelligence algorithms
able to strongly solve Connect Four are minimax or negamax, with optimizations that
include alpha-beta pruning, move ordering, and transposition tables. The code for solving
Connect Four with these methods is also the basis for the Fhourstones integer performance
benchmark.

The solved conclusion for Connect Four is first player win. With perfect play, the first player
can force a win, on or before the 41st move (ply) by starting in the middle column. The game
is a theoretical draw when the first player starts in the columns adjacent to the center. For the
edges of the game board, column 1 and 2 on left (or column 7 and 6 on right), the exact move-
value score for first player start is loss on the 40th move, and loss on the 42nd
move, respectively. In other words, by starting with the four outer columns, the first player
allows the second player to force a win.

9
Strategy :
1. If there is a winning move, take it.
2. If your opponent has a winning move, take the move so he can’t take it.
3. Take the center square over edges and corners.
4. Take corner squares over edges.
5. Take edges if they are the only thing available.

Apparently, this strategy won’t always work. In general, we must classify these sorts of rules
into two classes:
I) Rules that guarantee a certain result (and require proof that they do so)
II) Heuristic rules that are generally advantageous but are not without downfall (like
the strategy given above)

Terminology:
Threat:
A threat is a square that if taken by opponent forms a game-winning group of four
Useless Threat:
A useless threat is a threat that will never be able to be carried out by the opponent.
Odd and Even Threats:
The odd/evenness of a threat is determined by the row number.
Zugzwang:
The formal definition of this strange German word: a situation where a player is forced to make
a move when he would rather make no move at all. I In connect four, a player is able to “control
the zugzwang” if the player is able to guide the way odd and even squares are divided up among
players.
So, in conclusion, Zugzwang involves being able to divide up how even and odd
squares are distributed to the two players.

10
Figures:

Fig.1

11
Fig.2

12
Fig.3

13
Fig.4

14
Code:
import numpy as np
import pygame
import sys
import math

BLUE = (0,0,255)
BLACK = (0,0,0)
RED = (255,0,0)
YELLOW = (255,255,0)

ROW_COUNT = 6
COLUMN_COUNT = 7

def create_board():
board = np.zeros((ROW_COUNT,COLUMN_COUNT))
return board

def drop_piece(board, row, col, piece):


board[row][col] = piece

def is_valid_location(board, col):


return board[ROW_COUNT-1][col] == 0

def get_next_open_row(board, col):


for r in range(ROW_COUNT):
if board[r][col] == 0:
return r

15
def print_board(board):
print(np.flip(board, 0))

def winning_move(board, piece):


# Check horizontal locations for win
for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] ==
piece and board[r][c+3] == piece:
return True

# Check vertical locations for win


for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT-3):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] ==
piece and board[r+3][c] == piece:
return True

# Check positively sloped diaganols


for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT-3):
if board[r][c] == piece and board[r+1][c+1] == piece and
board[r+2][c+2] == piece and board[r+3][c+3] == piece:
return True

# Check negatively sloped diaganols


for c in range(COLUMN_COUNT-3):
for r in range(3, ROW_COUNT):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-
2][c+2] == piece and board[r-3][c+3] == piece:
return True

16
def draw_board(board):
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
pygame.draw.rect(screen, BLUE, (c*SQUARESIZE,
r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(screen, BLACK,
(int(c*SQUARESIZE+SQUARESIZE/2),
int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS)

for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
if board[r][c] == 1:
pygame.draw.circle(screen, RED,
(int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)),
RADIUS)
elif board[r][c] == 2:
pygame.draw.circle(screen, YELLOW,
(int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)),
RADIUS)
pygame.display.update()

board = create_board()
print_board(board)
game_over = False
turn = 0

pygame.init()

SQUARESIZE = 100

width = COLUMN_COUNT * SQUARESIZE


height = (ROW_COUNT+1) * SQUARESIZE

17
size = (width, height)

RADIUS = int(SQUARESIZE/2 - 5)

screen = pygame.display.set_mode(size)
draw_board(board)
pygame.display.update()

myfont = pygame.font.SysFont("monospace", 75)

while not game_over:

for event in pygame.event.get():


if event.type == pygame.QUIT:
sys.exit()

if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
posx = event.pos[0]
if turn == 0:
pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)),
RADIUS)
else:
pygame.draw.circle(screen, YELLOW, (posx,
int(SQUARESIZE/2)), RADIUS)
pygame.display.update()

if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
#print(event.pos)

18
# Ask for Player 1 Input
if turn == 0:
posx = event.pos[0]
col = int(math.floor(posx/SQUARESIZE))

if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 1)

No index entries found. if winning_move(board, 1):


label = myfont.render("Player 1 wins!!", 1, RED)
screen.blit(label, (40,10))
game_over = True

# # Ask for Player 2 Input


else:
posx = event.pos[0]
col = int(math.floor(posx/SQUARESIZE))

if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 2)

if winning_move(board, 2):
label = myfont.render("Player 2 wins!!", 1,
YELLOW)
screen.blit(label, (40,10))
game_over = True

print_board(board)

19
draw_board(board)

turn += 1
turn = turn % 2

if game_over:
pygame.time.wait(3000)

20
Conclusion :
Project based on open source i.e python and pygame is completed successfully .

References :
 https://www.pygame.org/news
 http://web.mit.edu/sp.268/www/2010/connectFourSlides.pdf
 https://www.wikipedia.com

21

You might also like