0
   

Connect 4 need a function that sees if the computer has a 2 move forced win

 
 
Reply Mon 5 Dec, 2011 12:16 pm
//1. need a function that sees if the computer has a 2 move forced win. if so, take the first of those 2 moves.
//2. See if the opponent has a 2 move forced win. if so, hten block the first of thos two moves.

// The gameboard array --this holds the game status
var gaBoard = [['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o']];


var gcPlayer = "r"; // current player color
var gbGameOver = false;

//Loading the Gameboard
function vLoad() {

var strTable, i, j, strID, strOver, strOut, strTitle, strHover, strPush, strArrow;

strTable = "<table cellpadding='0' cellspacing='0'>";

//put a row in the table for the title

for (i = 0; i < 1; i++) {

strTable += "<tr>";

strTitle = "<h1 style= 'color:Orange; font-family:Broadway'>Connect Four</h1>";

//put in the title

strTable += strTD(7, strTitle);

strTable += "</tr>";

// Buttons area
strTable += "<tr>";

strTable += strTD(4, strButton("New Game", "vReset()"));
strTable += strTD(3, strButton("Computer Move", "vComputerMove()"));

strTable += "</tr>";

}

//Arrows Row

strTable += "<tr>";
//put in the columns
for (j = 0; j < 7; j++) {
strID = "img" + j;
strHover = "vHover(\"" + strID + "\")";
strPush = "vPush(\"" + strID + "\"," + j + ")";
strArrow = "vArrow(\"" + strID + "\")";
strTable += strTD(1, strImage("arrow.jpg", strID, strPush, strHover, strArrow));
}
strTable += "</tr> <tr><td colspan='7'><hr/></td></tr>";



//put a row in the table for the gameboard
for (i = 5; i >= 0; i--) {
strTable += "<tr>";
//put in the columns
for (j = 0; j < 7; j++) {
strID = "img" + i + j;

strTable += strTD(1, strImage("open.jpg", strID, "", "", ""));
}
strTable += "</tr>";
}

//put a row in the table for the text area
for (i = 0; i < 1; i++) {
strTable += "<tr>";
strTable += strTD(7, strTextArea(16, 66));
strTable += "</tr>";
}

strTable += "</table>";
frm1.innerHTML = strTable;
}

//***************************************************************************************************

//Functions for Modifying controls
function vHover(strID) {
document.getElementById(strID).src = "hover.jpg";
}
function vArrow(strID) {
document.getElementById(strID).src = "arrow.jpg";
}

function vMakeRed(strID) {
document.getElementById(strID).src = "red.jpg";
}

function vMakeBlack(strID) {
document.getElementById(strID).src = "black.jpg";
}

function vMakeOpen(strID) {
document.getElementById(strID).src = "open.jpg";
}

function iTopSlot(iCol) {
var iRow, iCol;

iRow = 0;

while ( (iRow < 6) && (gaBoard[iRow][iCol] != 'o'))
iRow++;
return iRow;
}

function vPush(strID, iCol) {
var iRow, iCol;

document.getElementById(strID).src = "push.jpg";
/*
if (gbGameOver) {
return 0;
}
*/
// see if we can drop a piece into the selected column
if (gaBoard[5][iCol] == 'o') {
iRow = iTopSlot(iCol);
gaBoard[iRow][iCol] = gcPlayer;
strID = "img" + iRow + iCol;
// switch players
if (gcPlayer == "r")
{
vMakeRed(strID);
if (bWinner(gcPlayer)) {
alert(gcPlayer + " has won the game.");
frm1.taOutput.value += gcPlayer + " has won the game.";
gbGameOver = true;
}
gcPlayer = "b";
}
else
{
vMakeBlack(strID);
if (bWinner(gcPlayer)) {
frm1.taOutput.value += gcPlayer + " has won the game.";
alert(gcPlayer + " has won the game.");
gbGameOver = true;
}
gcPlayer = "r";
}
}

else {
frm1.taOutput.value += "That column is full - try again.\n";
}
}

function bWinner(cPlayer)
{
var iCol, iRow;

//Horizontal Win
for (iRow = 0; iRow < 6; iRow++)
{
for (iCol = 0; iCol < 4; iCol++)
{
if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow][iCol + 1] == cPlayer) &&
(gaBoard[iRow][iCol + 2] == cPlayer) && (gaBoard[iRow][iCol + 3] == cPlayer))
return true;

}


}

//Verticle Win
for (iRow = 0; iRow < 3; iRow++) {
for (iCol = 0; iCol < 7; iCol++) {
if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow + 1][iCol] == cPlayer) &&
(gaBoard[iRow + 2][iCol] == cPlayer) && (gaBoard[iRow + 3][iCol] == cPlayer))
return true;

}


}

//diagonal win Positive Slope
for (iRow = 0; iRow < 3; iRow++) {
for (iCol = 0; iCol < 4; iCol++) {
if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow + 1][iCol + 1] == cPlayer) &&
(gaBoard[iRow + 2][iCol + 2] == cPlayer) && (gaBoard[iRow + 3][iCol + 3] == cPlayer))
return true;

}


}

//diagonal win Negative Slope
for (iRow = 3; iRow < 6; iRow++) {
for (iCol = 0; iCol < 4; iCol++) {
if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow - 1][iCol + 1] == cPlayer) &&
(gaBoard[iRow - 2][iCol + 2] == cPlayer) && (gaBoard[iRow - 3][iCol + 3] == cPlayer))
return true;

}

}


return false;
}

function vComputerMove(){
var iRow,iCol,iWinCol;

//Try to win
iWinCol = -1;
iLoseCol = -1;
for(iCol = 0; iCol < 7; iCol++)
{
iRow = iTopSlot(iCol)
if(iRow < 6)
{
gaBoard[iRow][iCol] = gcPlayer;

if(bWinner(gcPlayer))
iWinCol = iCol;

gaBoard[iRow][iCol] = 'o';
}
}

if (iWinCol > -1) {
vPush("img" + iWinCol, iWinCol);
}


//Try to Block
if (gcPlayer == 'b') {
for (iCol = 0; iCol < 7; iCol++) {
iRow = iTopSlot(iCol)
if (iRow < 6) {
gaBoard[iRow][iCol] = 'r';

if (bWinner('r'))
iLoseCol = iCol;

gaBoard[iRow][iCol] = 'o';
}
}
}
else if (gcPlayer == 'r') {
for (iCol = 0; iCol < 7; iCol++) {
iRow = iTopSlot(iCol)
if (iRow < 6) {
gaBoard[iRow][iCol] = 'b';

if (bWinner('b'))
iLoseCol = iCol;

gaBoard[iRow][iCol] = 'o';
}
}
}

if (iLoseCol > -1) {
vPush("img" + iLoseCol, iLoseCol);
}

else
vRandomMove();
}

function vRandomMove() {
var iRandom, iRow;

iRow = 6;

while (iRow == 6) {
iRandom = (Math.floor(Math.random() * 7));
iRow = iTopSlot(iRandom);
}

vPush("img" + iRandom, iRandom);
}



function vReset() {
for (i = 5; i >= 0; i--) {
for (j = 0; j < 7; j++) {
gaBoard[j] = 'o';
strID = "img" + i + j;
vMakeOpen(strID);
}
}

frm1.taOutput.value = "";

gcPlayer = "r";
}

//***************************************************************************************************

//Functions for making form controls

//Button Maker
function strButton(strValue, strOnclick) {
return "<input type='button' value='" + strValue + "' onclick='" + strOnclick + "' />"
}

//Image Maker
function strImage(strSource, strID, strOnclick, strMouseOver, strMouseOut) {

var strReturn;

strReturn = "<img src='" + strSource + "' id='" + strID + "' onclick='" + strOnclick + "' ";
strReturn += "onmouseover='" + strMouseOver + "' onmouseout='" + strMouseOut + "' />"

return strReturn;
}

//TD Maker
function strTD(iColspan, strContent) {
return "<td colspan='" + iColspan + "'>" + strContent + "</td>";
}

//Text Area Maker
function strTextArea(iRows, iCol) {
var strReturn;

strReturn = "<textarea id='taOutput' ";
strReturn += "' rows= '" + iRows + "' cols='" + iCol + "'readonly='readonly'>";
strReturn += "</textarea>";

return strReturn;
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 1,365 • Replies: 3
No top replies

 
engineer
 
  1  
Reply Mon 5 Dec, 2011 02:30 pm
@Franklin23,
There are only six possible moves on a turn, six counters and six more responses to those counters. Run a loop for move A from 1 to 6 for move one. For each value in that loop if there is a C for any B that wins, you have a two move forced win. Do the same for the opponent for part two.
0 Replies
 
Franklin23
 
  1  
Reply Wed 7 Dec, 2011 11:21 am
@Franklin23,
How would you code that?
0 Replies
 
engineer
 
  1  
Reply Wed 7 Dec, 2011 12:42 pm
@Franklin23,
I'll let you do the actual coding, but the structure should look something like this

Code:move1 = 1
do while move1 <= 7 ; step through player one moves
for move2=1 to 7 ; step through player two moves
localwin = false
for move3= 1 to 7 ; step through player one responses
evaluate for win using bWinner
if win then localwin = true
loop ; if no localwin at this point, move1 is a failure, break out of the loops,
; increment move1 and try the next move
loop ; if you have a local win for each variation of move 2 at this point, move1 is
; a win in two moves since all responses result in victory.
loop
0 Replies
 
 

Related Topics

unix script - Question by kumarbit70
 
  1. Forums
  2. » Connect 4 need a function that sees if the computer has a 2 move forced win
Copyright © 2021 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 11/27/2021 at 08:09:42