Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Python Language Connect Four We will be programming an interface to s

ID: 655646 • Letter: P

Question

Please use Python Language

Connect Four

We will be programming an interface to simulate a connect four game. Those unfamiliar with connect four should look here for more details. The entire game will be text based, with players taking turns entering moves. Your connect four program should first prompt the user for a number of rows, then a number of columns. If either of these values is less than five, the program should continue to ask the user for a row or column value until the user enters a number five or greater. If the user enters something that isn't a number, the program should continue to prompt them until they enter a number. Once a number of rows and number of columns has been successfully been entered, you should continue to prompt each player for a move in turn until a player has won or drawn. Once the game has ended, you should display the winner and present an option to play again by typing y, or to quit by typing n.

At any point during the game, the player whose turn it is can type "s". Typing "s" will immediately save the game to a text file in whatever fromat you choose. At the beginning of each run of the program, the program should give the player the option to load a game stored in a text file.

Your output should look EXACTLY as follows:

For a draw, it should print "The game is a draw!". Should the player enter an "n" at the end, the program may simply terminate. If a player tries to move in a row that is already full, the program should prompt them to "Please enter a valid choice: ".

Design Suggestions

The first step is not to panic! We are going to break what is an intimidating program down into something simple. For this project, you should represent the board as a 2D array of 0s, 1s, and -1s (where a 0 is a blank space, 1 is player 1's move, and a -1 is player 2's move). You will also need a way of keeping track of whose turn it is. Here is a list of methods you should have:

move(myboard, col, player)--myboard is your 2D array that is storing your board, col is the column a player is TRYING to move, and player is the player to move. If it is a valid move, your program should go ahead and change my board so that it now has the appropriate move in it, and return True. If it is not a valid move, myboard should return False. For example:

printboard(myboard)--prints myboard to the screen, in the format shown above (you will have to change the zeros to underscores, etc).

checkWin(myboard, lastColPlayed)--check win returns a 1 if player 1 has won, a -1 if player 2 has won, and 0 otherwise. lastColPlayed is the last valid move that was made. Knowing this makes detecting a win way easier (you ONLY have to look at four-in-a-row made by the piece that was just played, since otherwise the game would have ended earlier). Feel free to write helper functions if you want to break it down into horiziontal, vertical, and diagonals.

isDraw(myboard)--returns true if it's a draw

save(myBoard, turn, filename)--writes the board to a text file.

load(filename)--loads a game.

Explanation / Answer

mode = "win" #initial mode, should be "instruktion" but can be changed for testing
wires=loadFont ("Gulim-64.vlw")
kek = 1337#the score limit variable

points1 = 0 #player 1's score
points2 = 0 #player 2's score
skore1 = points1 #player 1's score as displayed on screen
skore2 = points2 #player 2's score as displayed on screen

win1 = 0 #player 1 wins a round
win2 = 0 #player 2 wins a round
win1text = "Player 1 wins round!"
win2text = "Player 2 wins round!"

viktory1 = 0
viktory2 = 0 #conditionals

viktoryspeech1 = "Player 1 is winner!" #player 1 wins
viktoryspeech2 = "Player 2 is winner!" #player 2 wins

stabber = 0 #y axis, adapting mouseY
jabber = 0 #x axis, adapting mouseX
turn = 1 #1 is player 1, 2 is player 2

ohbabyatriple = loadImage("mlgdew.png") #**** HER RIGHT IN THE *****

board = [ [0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0] ]

#board = [ [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0] ]

def setup():
size(1100,800)

def draw():
global mode,viktory1,viktory2
textFont(wires,100)
rectMode(CENTER)
if mode == "instruktion":#the welcome screen with instructions
background(200,200,200)
textSize(100)
fill(31,73,125)
text ("Welkom!",350,125)
textSize(40)
text("To Konnekt 4!",400,195)
text ("INSTRUKTIONS",390,265)
textSize(30)
#the following is instructional text
text ("1. Two players will first decide on a skore",290,320)
text ("2. To drop a piece, klik on a space on the bord",260,360)
text ("3. First player to konnekt 4 in any direktion wins the round",200,400)
text ("4. First player to reach skore limit wins the game",250,440)
text ("5. To enter a skore limit, from 1-9, press the respektiv" ,200,480)
text (" number on the keyboard.",370,510)
text ("WARNING: WATCH WHAT SKORE YOU PICK.",240,550)
text ("AND NEVER PRESS 'X'",400,590) #shmuck bait
if mode == "game":#the main game interface
global board
if viktory1 == 1:
mode = "kongrats"
elif viktory2 ==1:
mode = "kongrats"
elif viktory1 ==1 and viktory2 == 1:
mode = "kill"
background(200,200,200)
rectMode(CENTER)
fill(31,73,125)
textSize(100)
text("Konnekt 4",300,100)
text ("I",160,300)
textSize(60)
text("Skore",90,200)
textSize(80)
text(skore1,100,290)
text(skore2,200,290)
noFill()
translate(400,230) #shift the board, due to nature of generation

#generating board
for y in range(6): #y axis
for x in range(7): #x axis
if board[y][x] == 1 and turn!=0:
fill(137,2,6)
elif board[y][x] == 2 and turn!=0:
fill(6,2,137)
elif board[y][x] == 0:
noFill()

rect(x*100,y*80,100,80)
#each tile is 100x80

if mode == "win":
global ohbabyatriple,win1,win2,win1text,win2text
background(200,200,200)
textSize(100)
fill(31,73,125)
if win1 ==1:
text(win1text,130,100)
elif win2 ==1:
text(win2text,130,100)
else:
text("Oh hi, tester!",300,100)
textSize(40)
text("Press E to continue",370,175)
image(ohbabyatriple,200,300)
image(ohbabyatriple,875,300)
image(ohbabyatriple,530,300)

if mode == "draw":
global ohbabyatriple
background(200,200,200)
textSize(100)
fill(31,73,125)
text("It's a draw!",310,100)
textSize(40)
text("Press E to continue",370,175)
image(ohbabyatriple,200,300)
image(ohbabyatriple,875,300)
image(ohbabyatriple,530,300)

if mode =="kongrats": #victory screen
background(200,200,200)
textSize(100)
fill(31,73,125)
text("Kongratulations!",200,100)
textSize(60)
if viktory1 ==1:#player 1's win
text (viktoryspeech1,310,200)
text ("Have a nice day!",330,280)
elif viktory2 ==1:#player 2's win
text (viktoryspeech2,310,200)
text ("Have a nice day!",330,280)
else:
text ("Oh, this is just a test then?",200,200)
#^ because you normally only get the victory screen after one player wins

if mode =="OHGODWHY?!":#Shouldn't have pressed it.
doomedbirds= loadImage("birds_nooooo.jpg")
ffffuuuu = loadImage ("self_destruct.png")

background(200,200,200)
textSize(100)
fill(31,73,125)
text("YOU FOOL!",300,100)
text("YOU DOOMED US ALL!",22,222)
text("NOOOOOOOOOOOOOOOOOOOOOOO",22,333)
image(doomedbirds,560,420)
image(ffffuuuu,100,420)
mode = "kill"

if mode =="kill":#part of the don't-press-the-x code
exit()

def keyPressed():
global mode
global kek
global win1, win2, viktory1, viktory2
if key in '123456789' and key != 0 and mode == "instruktion":
kek = int (key)
mode = "game"
if key in 'eE' and mode == "win" or key in 'eE' and mode == "draw":
if win1 ==1 and points1 != kek:
win1 =0
elif win2 == 1 and points2 != kek:
win1 = 0
elif points1 ==kek:
viktory1 = 1
elif points2 == kek:
viktory2 = 1

mode = "game"


#WARNING: SELF DESTRUCT CODE
if key in 'xX':
mode = "OHGODWHY?!"

def mousePressed():
global jabber, stabber, turn
stabber = (mouseY-190)/80 #y axis
jabber =(mouseX-350)/100 #x axis

if mode == "game" and stabber >=0 and stabber <=5 and jabber >=0 and jabber<=6 and board[stabber][jabber] == 0:
board[stabber][jabber] = turn
print turn #extra info
#color that cell with player's color
if turn == 1:
turn = 2
elif turn == 2:
turn = 1
#cycling turns