winSub = function ( list ) {

if ( all ( list == "R" ) ) { return ( "R" ) }

else if ( all ( list == "B" ) ) { return ( "B" ) }

return ( "0" )

}

u = function ( gb, i, j ) {

if ( i - 3 >= 1 ) {

return ( gb [ i : ( i - 3 ) ,j ] )

}

else {

return ( rep ( "0" , 4 ) )

}

}

d = function ( gb, i, j ) {

if ( i + 3 <= 6 ) {

return ( gb [ i : ( i + 3 ) ,j ] )

}

else {

return ( rep ( "0" , 4 ) )

}

}

r = function ( gb, i, j ) {

if ( j + 3 <= 7 ) {

return ( gb [ i,j : ( j + 3 ) ] )

}

else {

return ( rep ( "0" , 4 ) )

}

}

l = function ( gb, i, j ) {

if ( j - 3 >= 1 ) {

return ( gb [ i, ( j - 3 ) : j ] )

}

else {

return ( rep ( "0" , 4 ) )

}

}

ur = function ( gb, i, j ) {

f = rep ( "0" , 4 )

if ( any ( u ( gb, i, j ) != f ) & any ( r ( gb, i, j ) != f ) ) {

return ( c ( gb [ i,j ] , gb [ i - 1 , j + 1 ] , gb [ i - 2 ,j + 2 ] , gb [ i - 3 ,j + 3 ] ) )

}

else {

return ( rep ( "0" , 4 ) )

}

}

ul = function ( gb, i, j ) {

f = rep ( "0" , 4 )

if ( any ( u ( gb, i, j ) != f ) & any ( l ( gb, i, j ) != rep ( "0" , 4 ) ) ) {

return ( c ( gb [ i,j ] , gb [ i - 1 , j - 1 ] , gb [ i - 2 ,j - 2 ] , gb [ i - 3 ,j - 3 ] ) )

}

else {

return ( rep ( "0" , 4 ) )

}

}

dr = function ( gb, i, j ) {

f = rep ( "0" , 4 )

if ( any ( d ( gb, i, j ) != f ) & any ( r ( gb, i, j ) != rep ( "0" , 4 ) ) ) {

return ( c ( gb [ i,j ] , gb [ i + 1 , j + 1 ] , gb [ i + 2 ,j + 2 ] , gb [ i + 3 ,j + 3 ] ) )

}

else {

return ( rep ( "0" , 4 ) )

}

}

dl = function ( gb, i, j ) {

f = rep ( "0" , 4 )

if ( any ( d ( gb, i, j ) != f ) & any ( l ( gb, i, j ) != rep ( "0" , 4 ) ) ) {

return ( c ( gb [ i,j ] , gb [ i + 1 , j - 1 ] , gb [ i + 2 ,j - 2 ] , gb [ i + 3 ,j - 3 ] ) )

}

else {

return ( rep ( "0" , 4 ) )

}

}

drawWinLine = function ( i,j,test ) {

if ( test == 1 ) {

lines ( c ( xIndex ( j ) ,xIndex ( j ) ) , c ( yIndex ( i ) , yIndex ( i - 3 ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 2 ) {

lines ( c ( xIndex ( j ) ,xIndex ( j + 3 ) ) , c ( yIndex ( i ) , yIndex ( i ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 3 ) {

lines ( c ( xIndex ( j ) ,xIndex ( j ) ) , c ( yIndex ( i ) , yIndex ( i + 3 ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 4 ) {

lines ( c ( xIndex ( j ) , xIndex ( j - 3 ) ) , c ( yIndex ( i ) , yIndex ( i ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 5 ) {

lines ( c ( xIndex ( j ) , xIndex ( j + 3 ) ) , c ( yIndex ( i ) , yIndex ( i - 3 ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 6 ) {

lines ( c ( xIndex ( j ) , xIndex ( j - 3 ) ) , c ( yIndex ( i ) , yIndex ( i - 3 ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 7 ) {

lines ( c ( xIndex ( j ) , xIndex ( j + 3 ) ) , c ( yIndex ( i ) , yIndex ( i + 3 ) ) ,lwd = 4 , col = "blue" )

}

else if ( test == 8 ) {

lines ( c ( xIndex ( j ) , xIndex ( j - 3 ) ) , c ( yIndex ( i ) , yIndex ( i + 3 ) ) ,lwd = 4 , col = "blue" )

}

}

winSquare = function ( gb, i, j ) {

if ( gb [ i,j ] == "0" ) {

return ( "0" )

}

testFunc = 1

for ( test in c ( u,r,d,l,ur,ul,dr,dl ) ) {

if ( winSub ( test ( gb,i,j ) ) != "0" ) {

drawWinLine ( i,j,testFunc )

return ( winSub ( test ( gb,i,j ) ) )

}

testFunc = testFunc + 1

}

return ( "0" )

}

checkWin = function ( gb ) {

for ( row in 1 : 6 ) {

for ( col in 1 : 7 ) {

tmp = winSquare ( gb, row , col )

if ( tmp != "0" ) {

return ( tmp )

}

}

}

return ( "0" )

}

winSquareAI = function ( gb, i, j ) {

if ( gb [ i,j ] == "0" ) {

return ( "0" )

}

testFunc = 1

for ( test in c ( u,r,d,l,ur,ul,dr,dl ) ) {

if ( winSub ( test ( gb,i,j ) ) != "0" ) {

return ( winSub ( test ( gb,i,j ) ) )

}

testFunc = testFunc + 1

}

return ( "0" )

}

checkWinAI = function ( gb ) {

for ( row in 1 : 6 ) {

for ( col in 1 : 7 ) {

tmp = winSquareAI ( gb, row , col )

if ( tmp != "0" ) {

return ( tmp )

}

}

}

return ( "0" )

}

flipPlayer = function ( p ) {

if ( p == "R" ) { return ( "B" ) }

return ( "R" )

}

getRow = function ( gb, j ) {

return ( max ( which ( gb [ ,j ] == "0" ) ) )

}

commitMove = function ( gb,i,j,player ) {

if ( ( i <= 6 ) & ( j <= 7 ) & ( i >= 1 ) & ( j >= 1 ) & ( gb [ i,j ] == "0" ) ) {

gb [ i,j ] = player

return ( gb )

}

print ( "invalid move" )

return ( "0" )

}

yIndex = function ( i ) {

return ( 9 - 3 * i + 18 / 12 )

}

xIndex = function ( j ) {

return ( - 9 + ( 18 / 7 ) * j - 18 / 14 )

}

plotPiece = function ( p, i, j ) {

if ( p == "R" ) {

points ( x = xIndex ( j ) , y = yIndex ( i ) , cex = 8 , col = "red" , pch = 16 )

}

else if ( p == "B" ) {

points ( x = xIndex ( j ) , y = yIndex ( i ) , cex = 8 , col = "black" , pch = 16 )

}

}

plotMenu = function ( ) {

points ( matrix ( c ( seq ( - 5 , 5 ) , rep ( - 13 , 11 ) ) , ncol = 2 ) , cex = 12 , col = "white" , pch = 15 )

points ( x =- 4 , y = - 13 , pch = 16 , col = "lightblue" , cex = 12 )

points ( x = 4 , y = - 13 , pch = 16 , col = "lightblue" , cex = 12 )

points ( x = 0 , y = - 13.5 , pch = 16 , col = "lightblue" , cex = 8 )

text ( x = - 4 , y = - 12.5 , labels = "New Game:" , cex = 1 )

text ( x = - 4 , y = - 13.5 , labels = "1 Player" , cex = 1 )

text ( x = 4 , y = - 12.5 , labels = "New Game:" , cex = 1 )

text ( x = 4 , y = - 13.5 , labels = "2 Player" , cex = 1 )

text ( x = 0 , y = - 13.5 , labels = "AI Game" , cex = 0.95 )

}

plotGameBoard = function ( gb, text ) {

plot ( c ( - 10 , 10 ) , c ( - 10 , 10 ) , type = "n" , main = text , xaxt = "n" , yaxt = "n" , xlab = "" , ylab = "" )

data = cbind ( ( 9 / 7 ) * sort ( c ( seq ( - 7 , 7 , 2 ) , seq ( - 7 , 7 , 2 ) ) ) , rep ( c ( - 9 , 9 ) , 8 ) )

for ( i in seq ( 1 , 16 , 2 ) ) { lines ( data [ i : ( i + 1 ) , ] ) }

data = cbind ( rep ( c ( - 9 , 9 ) ) , c ( - 9 , - 9 , - 6 , - 6 , - 3 , - 3 , 0 , 0 , 3 , 3 , 6 , 6 , 9 , 9 ) )

for ( i in seq ( 1 , 14 , 2 ) ) { lines ( data [ i : ( i + 1 ) , ] ) }

for ( row in 1 : 6 ) {

for ( col in 1 : 7 ) {

piece = gb [ row , col ]

plotPiece ( piece, row , col )

}

}

}

animateMove = function ( gb,newGb, text , col , player, endRow ) {

plotGameBoard ( gb, text )

movement = c ( xIndex ( col ) , yIndex ( 1 ) + 2 , 0 )

if ( player == "R" ) { color = "red" }

else { color = "black" }

while ( movement [ 2 ] > yIndex ( endRow ) ) {

plotGameBoard ( gb, text )

points ( movement [ 1 ] , movement [ 2 ] , cex = 8 , col = color, pch = 16 )

movement [ 2 ] = movement [ 2 ] - movement [ 3 ]

movement [ 3 ] = movement [ 3 ] + 0.07

}

plotGameBoard ( newGb, text )

}

handleGameBoardInput = function ( input, moves, AILevel ) {

x = min ( which ( input [ 1 ] <= ( 9 / 7 ) * seq ( - 7 , 7 , 2 ) [ 2 : 8 ] ) )

if ( any ( x == seq ( 1 , 7 ) ) & input [ 1 ] > - 9 ) {

return ( x )

}

else { return ( getInput ( moves, AILevel ) ) }

}

handleMenuInput = function ( input,moves, AILevel ) {

if ( input [ 1 ] < - 2 ) {

New1Player ( AILevel )

}

else if ( input [ 1 ] > 2 ) {

New2Player ( AILevel )

}

else if ( input [ 1 ] < 1 & input [ 1 ] > - 1 & input [ 2 ] < - 13 & input [ 2 ] > - 14 ) {

New0Player ( AILevel )

}

else { return ( getInput ( moves, AILevel ) ) }

}

getInput = function ( moves = TRUE, AILevel = 1 ) {

input = as. numeric ( locator ( 1 ) )

if ( input [ 2 ] > - 9 & input [ 2 ] < 9 & moves == TRUE ) {

return ( handleGameBoardInput ( input, moves, AILevel ) )

}

else if ( input [ 1 ] < 5.5 & input [ 2 ] < - 10 & input [ 1 ] > - 5.5 & moves == FALSE ) {

return ( handleMenuInput ( input,moves, AILevel ) )

}

else return ( getInput ( moves, AILevel ) )

}

createMoveArrayTree = function ( gameBoard, player ) {

dataArray = array ( rep ( "0" , 6 * 7 * 7 ) )

dim ( dataArray ) = c ( 6 , 7 , 7 )

for ( i in 1 : 7 ) {

moveRow = getRow ( gameBoard,i )

if ( moveRow != - Inf ) {

dataArray [ ,,i ] = commitMove ( gameBoard, moveRow, i, player )

}

else {

dataArray [ ,,i ] = NaN

}

}

return ( dataArray )

}

randomChoice = function ( listVar ) {

if ( length ( listVar ) == 0 ) {

return ( NaN )

}

else if ( length ( listVar ) == 1 ) {

return ( listVar [ 1 ] )

}

else {

return ( sample ( listVar ) [ 1 ] )

}

}

getAIMove = function ( gameBoard, oneMoveArray, twoMoveArray, threeMoveArray, fourMoveArray, winPlayer, level = 2 ) {

oneMoveWinList = c ( )

twoMoveLooseList = c ( )

threeMoveWinList = c ( )

fourMoveLooseList = c ( )

winMoveMatrix = matrix ( c ( 99 , 99 , 99 ) , ncol = 3 )

for ( m1 in 1 : 7 ) {

moveBoard = oneMoveArray [ ,,m1, 1 , 1 , 1 , 1 ]

if ( checkWinAI ( moveBoard ) != "0" ) {

oneMoveWinList = c ( oneMoveWinList, m1 )

}

}

if ( length ( oneMoveWinList ) > 0 ) {

return ( randomChoice ( oneMoveWinList ) )

}

for ( m1 in 1 : 7 ) {

for ( m2 in 1 : 7 ) {

moveBoard = twoMoveArray [ ,,m1,m2, 1 , 1 , 1 ]

win = checkWinAI ( moveBoard )

if ( win == flipPlayer ( winPlayer ) ) {

twoMoveLooseList = c ( twoMoveLooseList, m1 )

winMoveMatrix = rbind ( winMoveMatrix, c ( m1,m2, 99 ) )

}

}

}

for ( m1 in 1 : 7 ) {

for ( m2 in 1 : 7 ) {

for ( m3 in 1 : 7 ) {

moveBoard = threeMoveArray [ ,,m1,m2,m3, 1 , 1 ]

win = checkWinAI ( moveBoard )

if ( win == winPlayer ) {

threeMoveWinList = c ( threeMoveWinList, m1 )

winMoveMatrix = rbind ( winMoveMatrix, c ( m1,m2,m3 ) )

}

}

}

}

if ( level == 2 ) {

for ( m1 in 1 : 7 ) {

for ( m2 in 1 : 7 ) {

for ( m3 in 1 : 7 ) {

for ( m4 in 1 : 7 ) {

moveBoard = fourMoveArray [ ,,m1,m2,m3,m4, 1 ]

win = checkWinAI ( moveBoard )

if ( win == winPlayer & ! rowOfMatrix ( c ( m1,m2,m3 ) , winMoveMatrix ) ) {

fourMoveLooseList = c ( fourMoveLooseList, m1 )

}

}

}

}

}

}

goodMoves1 = setdiff ( threeMoveWinList, twoMoveLooseList )

goodMoves2 = setdiff ( threeMoveWinList, union ( twoMoveLooseList, fourMoveLooseList ) )

bestMoves = intersect ( goodMoves1, goodMoves2 )

neutralMoves = setdiff ( getMoves ( gameBoard ) , twoMoveLooseList )

move = NaN

if ( length ( goodMoves2 ) > 0 ) {

move = randomChoice ( goodMoves2 )

}

else if ( length ( goodMoves1 ) > 0 ) {

move = randomChoice ( goodMoves1 )

}

else {

move = randomChoice ( neutralMoves )

}

if ( is. na ( move ) ) {

move = randomChoice ( getMoves ( gameBoard ) )

}

if ( is. na ( move ) ) {

getInput ( )

}

return ( move )

}

getMoves = function ( gameboard ) {

a = gameboard [ 1 , ]

if ( ! any ( a == "0" ) ) {

return ( NaN )

}

else {

return ( which ( a == "0" ) )

}

}

makeAIMove = function ( gameBoard, player, AILevel, n = 5 ) {

winPlayer = player

dataArray = array ( rep ( "0" , ( ( 7 ^ n ) * 6 * 7 ) ) )

dim ( dataArray ) = c ( 6 , rep ( 7 ,n + 1 ) )

dataArray [ ,,,,,, ] = createMoveArrayTree ( gameBoard, player )

oneMoveArray = dataArray

player = flipPlayer ( player )

for ( m1 in 1 : 7 ) {

gameBoard2 = dataArray [ ,,m1, 1 , 1 , 1 , 1 ]

dataArray [ ,,m1,,,, ] = createMoveArrayTree ( gameBoard2, player )

}

twoMoveArray = dataArray

player = flipPlayer ( player )

for ( m1 in 1 : 7 ) {

for ( m2 in 1 : 7 ) {

gameBoard3 = dataArray [ ,,m1,m2, 1 , 1 , 1 ]

dataArray [ ,,m1,m2,,, ] = createMoveArrayTree ( gameBoard3, player )

}

}

threeMoveArray = dataArray

player = flipPlayer ( player )

for ( m1 in 1 : 7 ) {

for ( m2 in 1 : 7 ) {

for ( m3 in 1 : 7 ) {

gameBoard5 = dataArray [ ,,m1,m2,m3, 1 , 1 ]

dataArray [ ,,m1,m2,m3,, ] = createMoveArrayTree ( gameBoard5, player )

}

}

}

fourMoveArray = dataArray

return ( getAIMove ( gameBoard, oneMoveArray, twoMoveArray, threeMoveArray, fourMoveArray, winPlayer, AILevel ) )

# if (class(possMove) != "matrix"){return(possMove)}

#

#

#

# player = flipPlayer(player)

# for (m1 in 1:7){

# for (m2 in 1:7){

# for (m3 in 1:7){

# for (m4 in 1:7){

# gameBoard6 = dataArray[,,m1,m2,m3,m4,1]

# dataArray[,,m1,m2,m3,m4,] = createMoveArrayTree(gameBoard6, player)

# }

# }

# }

# }

#

# possMove = getAIMove(winPlayer, dataArray, 5)

# if (class(possMove) != "matrix"){

# return(possMove)

# }

# else{

# possMove = possMove[,2:length(possMove)]

# if (length(possMove) < 7){

# allMoves = getMoves(gameBoard)

# goodMoves = setdiff(allMoves, possMove)

# return(sample(goodMoves)[1])

# }

# else{

# allMoves = getMoves(gameBoard)

# return(sample(allMoves)[1])

# }

# }

}

#gameBoard = matrix(rep("0", 7*6), ncol = 7)

#gameBoard[6,4] = "B"

#gameBoard[6,5] = "B"

#gameBoard[6,2] = "B"

#gameBoard[6,1] = "R"

#gameBoard[6,7] = "R"

#print(makeAIMove(gameBoard, "R"))

New2Player = function ( AILevel ) {

gameBoard = matrix ( rep ( "0" , 7 * 6 ) , ncol = 7 )

player = "B"

repeat {

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

completedMove = F

while ( completedMove == F ) {

moveColumn = getInput ( )

moveRow = getRow ( gameBoard,moveColumn )

newBoard = commitMove ( gameBoard, moveRow, moveColumn, player )

if ( length ( newBoard ) != 1 ) {

animateMove ( gameBoard,newBoard, paste ( "Player: " , player ) , moveColumn, player, moveRow )

completedMove = T

gameBoard = newBoard

}

}

player = flipPlayer ( player )

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

winning = checkWin ( gameBoard )

if ( winning != "0" ) {

if ( winning == "R" ) {

text ( 0 , 0 , "Red Wins!" , cex = 4.6 , col = "white" )

print ( "Red wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

text ( 0 , 0 , "Black Wins!" , cex = 4.8 , col = "white" )

print ( "Black wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

}

}

New1Player = function ( AILevel ) {

gameBoard = matrix ( rep ( "0" , 7 * 6 ) , ncol = 7 )

player = "B"

repeat {

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

completedMove = F

while ( completedMove == F ) {

moveColumn = getInput ( )

moveRow = getRow ( gameBoard,moveColumn )

newBoard = commitMove ( gameBoard, moveRow, moveColumn, player )

if ( length ( newBoard ) != 1 ) {

animateMove ( gameBoard,newBoard, paste ( "Player: " , player ) , moveColumn, player, moveRow )

completedMove = T

gameBoard = newBoard

}

}

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

winning = checkWin ( gameBoard )

if ( winning != "0" ) {

if ( winning == "R" ) {

text ( 0 , 0 , "Red Wins!" , cex = 4.6 , col = "white" )

print ( "Red wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

text ( 0 , 0 , "Black Wins!" , cex = 4.8 , col = "white" )

print ( "Black wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

player = flipPlayer ( player )

plotGameBoard ( gameBoard, paste ( "Player: " , player, "is thinking." ) )

AIMoveColumn = makeAIMove ( gameBoard, player, AILevel )

print ( paste ( "AI chooses: " , AIMoveColumn ) )

moveRow = getRow ( gameBoard,AIMoveColumn )

newBoard = commitMove ( gameBoard, moveRow, AIMoveColumn, player )

animateMove ( gameBoard,newBoard, paste ( "Player: " , player ) , AIMoveColumn, player, moveRow )

gameBoard = newBoard

player = flipPlayer ( player )

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

winning = checkWin ( gameBoard )

if ( winning != "0" ) {

if ( winning == "R" ) {

text ( 0 , 0 , "Red Wins!" , cex = 4.6 , col = "white" )

print ( "Red wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

text ( 0 , 0 , "Black Wins!" , cex = 4.8 , col = "white" )

print ( "Black wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

}

}

rowOfMatrix = function ( rowVar, matrixVar ) {

for ( row in 1 : nrow ( matrixVar ) ) {

if ( all ( rowVar == matrixVar [ row , ] ) ) {

return ( TRUE )

}

}

return ( FALSE )

}

New0Player = function ( AILevel ) {

gameBoard = matrix ( rep ( "0" , 7 * 6 ) , ncol = 7 )

player = "B"

while ( TRUE ) {

plotGameBoard ( gameBoard, paste ( "Player: " , player, "is thinking." ) )

AIMoveColumn = makeAIMove ( gameBoard, player, AILevel )

print ( paste ( "AI chooses: " , AIMoveColumn ) )

moveRow = getRow ( gameBoard,AIMoveColumn )

newBoard = commitMove ( gameBoard, moveRow, AIMoveColumn, player )

animateMove ( gameBoard,newBoard, paste ( "Player: " , player ) , AIMoveColumn, player, moveRow )

gameBoard = newBoard

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

winning = checkWin ( gameBoard )

if ( winning != "0" ) {

if ( winning == "R" ) {

text ( 0 , 0 , "Red Wins!" , cex = 4.6 , col = "white" )

print ( "Red wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

text ( 0 , 0 , "Black Wins!" , cex = 4.8 , col = "white" )

print ( "Black wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

player = flipPlayer ( player )

plotGameBoard ( gameBoard, paste ( "Player: " , player, "is thinking." ) )

AIMoveColumn = makeAIMove ( gameBoard, player, AILevel )

print ( paste ( "AI chooses: " , AIMoveColumn ) )

moveRow = getRow ( gameBoard,AIMoveColumn )

newBoard = commitMove ( gameBoard, moveRow, AIMoveColumn, player )

animateMove ( gameBoard,newBoard, paste ( "Player: " , player ) , AIMoveColumn, player, moveRow )

gameBoard = newBoard

plotGameBoard ( gameBoard, paste ( "Player: " , player ) )

winning = checkWin ( gameBoard )

if ( winning != "0" ) {

if ( winning == "R" ) {

text ( 0 , 0 , "Red Wins!" , cex = 4.6 , col = "white" )

print ( "Red wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

text ( 0 , 0 , "Black Wins!" , cex = 4.8 , col = "white" )

print ( "Black wins!" )

plotMenu ( )

getInput ( FALSE, AILevel )

}

player = flipPlayer ( player )

}

}

plotAIChoiceMenu = function ( ) {

text ( x = 0 , y = 6 , labels = "Choose an AI Level:" , cex = 2 )

text ( x = 0 , y = 2 , labels = "AI Level 1" , cex = 3 )

text ( x = 0 , y = - 2 , labels = "AI Level 2" , cex = 3 )

}

getAIChoiceInput = function ( ) {

input = as. numeric ( locator ( 1 ) )

if ( input [ 2 ] > 1.5 & input [ 2 ] < 2.5 & input [ 1 ] > - 6 & input [ 1 ] < 6 ) {

print ( "AI Level 1 (fast)" )

return ( 1 )

}

else if ( input [ 2 ] < - 1.5 & input [ 2 ] > - 2.5 & input [ 1 ] > - 6 & input [ 1 ] < 6 ) {

print ( "AI Level 2 (slow)" )

return ( 2 )

}

else ( return ( getAIChoiceInput ( ) ) )

}

NewGame = function ( ) {

dev. new ( width = 7 , height = 7 )

options ( locatorBell = F )

par ( bg = "#DDDDDD" )

par ( xpd = TRUE )

gameBoard = matrix ( rep ( "0" , 7 * 6 ) , ncol = 7 )

plotGameBoard ( gameBoard, "Welcome to Connect 4!" )

plotAIChoiceMenu ( )

AILevel = getAIChoiceInput ( )

plotGameBoard ( gameBoard, "Welcome to Connect 4!" )

plotMenu ( )

while ( TRUE ) {

getInput ( FALSE, AILevel )

}

}