SoFunction
Updated on 2025-03-02

Example of Gozi chess game implemented by jQuery

This article describes the Gozi game implemented by jQuery. Share it for your reference. The details are as follows:

This is a very good code, but the artificial intelligence is a little bit bad

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<title>Gozi Chess</title>
<style type="text/css">
div{margin:0;padding:0;}
{width:561px; height:561px; border:1px solid #ccc; margin:0 auto;}
 div{ width:31px; height:31px; border:1px solid #ccc; float:left; cursor:pointer; background-repeat:no-repeat; }
 .person { background-image:url('images/1/files/demo/')}
 .machine{ background-image:url('images/1/files/demo/')}
 .person_star{background-image:url('images/1/files/demo/white_star.jpg')}
 .machine_star{background-image:url('images/1/files/demo/black_star.jpg')}
{ display:block; margin:0 auto; margin-top:8px;width:70px}
</style>
</head>
<body>
<div class='board' id='board'>
</div>
<input type='button' value='Start the game' onclick="initGame();
='restart'" class='ipt'/>
<script type='text/javascript'>
var TRANSVERSE = 16;
var VERTICAL = 16;
var LEFT = 1;
var RIGHT = 2;
var TOP = 3;
var BOTTOM = 4;
var LEFT_TOP = 5;
var LEFT_BOTTOM = 6;
var RIGHT_TOP = 7;
var RIGHT_BOTTOM = 8;
var Chess = function()
{
 var owner = '';
 var victory = false;
  = function(){return owner;};
  = function(value){owner = value;};
  = function(){ return victory;}
  = function(value){ victory = value; } 
}
var Board = function()
{
 var chessBoard = [];
 var isGameOver = false;
  = function(point)
 {
  var x =  , y = ;
  return chessBoard[y][x];
 }
  = function(chess , point)
 {
  var x =  , y = ;
  chessBoard[y][x] = chess;
 }
  = function(points)
 {
  for(var i = 0 ; i <  ; i ++)
  {
   for(var j = 0 ; j < points[i].length; j ++)
   {
    var chess = (points[i][j]);
    (true);
   }
  }
 }
  = function()
 {
  var avaiable = new Array;
  for(var y = 0 ; y <= VERTICAL ; y ++)
  {
   for(var x = 0 ; x <= TRANSVERSE ; x ++)
   {
    if(chessBoard[y][x]) continue;
    var point = {x : x , y : y};
    (point);
   }
  }
  return avaiable;
 }
  = function()
 {
  var map = {};
   for(var y = 0 ; y <= VERTICAL ; y ++)
   {
   for(var x = 0 ; x <= TRANSVERSE ; x++)
    {
    var chess = chessBoard[y][x];
     var value = '';
     if(chess)
     {
     value = ();
     if(()) value += '_star';
     }
     else 
     {
     value = '';
     }
     map[ x + ',' + y ] = value;
    }
   }
   return map;
 }
  = function()
 {
  return isGameOver = true;
 }
  = function()
 {
  return isGameOver;
 }
  = function(point , direction)
 {
  var next = {x :  , y : };
  switch(direction)
  {
   case LEFT :
     -= 1;
    break;
   case RIGHT:
     += 1;
    break;
   case TOP:
     -= 1;
    break;
   case BOTTOM:
     += 1;
    break;
   case LEFT_TOP:
    -= 1 , -= 1;
    break;
   case RIGHT_TOP:
     += 1 ,  -= 1;
    break;
   case LEFT_BOTTOM:
     -= 1 ,  += 1;
    break;
   case RIGHT_BOTTOM:
     += 1 ,  += 1;
    break;
   default :
    alert('Wrong direction');
  }
  return next;
 }
 var initialize = function()
 {
  for(var i = 0 ; i <= VERTICAL ; i++ ) ([]);
 } 
 initialize();
}
var Compute = function(role)
{
 var directions = [LEFT , TOP , RIGHT , BOTTOM , LEFT_TOP , LEFT_BOTTOM , RIGHT_TOP , RIGHT_BOTTOM];
 var score = 0;
 var self = this;
 this._computeScore = function(direction)
 {
  throw new Error('Unimplemented');
 }
 this._convertToPattern = function(chesslist)
 {
  return (chesslist)
 }
  = function(point)
 {
  score = 0;
  for(var i = 0, direction ; direction = directions[i++];)
  {
   score += this._computeScore(point , direction);
  } 
 }
  = function(refPoint)
 {
  return score ;
 }
}
var Five = function(role)
{
 (this, role);
 var computeScore1 = function(refPoint , direction)
 {
  var predefined = 'IIII';
  var chesslist = (refPoint , direction , 4);
  var pattern = (chesslist);
  if(predefined == pattern) return true;
  return false ;  
 }
 var computeScore2 = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 2);
  var next = (refPoint , (direction) , 2);
  var prevPattern = (prev);
  var nextPattern = (next);
  if(prevPattern == 'II' && nextPattern == 'II') return true;
  return false;
 }
 var computeScore3 = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 3);
  var next = (refPoint , (direction) , 1);
  var prevPattern = (prev);
  var nextPattern = (next);
 if(prevPattern == 'III' && nextPattern == 'I') return true;
 return false;  
 }
 this._computeScore = function(refPoint , direction)
 {
  if(computeScore1(refPoint , direction) || computeScore2(refPoint , direction) || computeScore3(refPoint , direction))
   return 100000;
  else return 0;
 }
}
var Four_Live = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var score = 0;
  var prev = (refPoint , direction , 4);
  var next = (refPoint , (direction), 1);
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);
  if(prevPattern == 'III0' && nextPattern == '0') score = 10000;  
 return score;  
 }
}
var Four_Live1 = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 3);
  var next = (refPoint , (direction) , 2);
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);  
  if(prevPattern == 'II0' && nextPattern == 'I0') return 10000;
  else return 0;
 }
}
var Tree_Live = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var score = 0;
  var prev = (refPoint , direction , 3);
  var next = (refPoint , (direction), 2);
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);
  if(prevPattern == 'II0' && nextPattern == '00')
   score += 1000;
  return score;
 }
}
var Tree_Live1 = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 2);
  var next = (refPoint , (direction), 3);
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);
 if(prevPattern == 'I0' && nextPattern == 'I00')
  return 1000
 else return 0;   
 }
}
var Two_Live = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 3);
  var next = (refPoint , (direction), 2); 
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);
 if(prevPattern == 'I00' && nextPattern == '00') return 100;
 else return 0;  
 }
}
var One_Live = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 3);
  var next = (refPoint , (direction), 3); 
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);
 if(prevPattern == '000' && nextPattern == '000') return 10;
 else return 0;  
 }
}
var Four_End = function(role)
{
 (this, role);
 this._computeScore = function(refPoint , direction)
 {
  var prev = (refPoint , direction , 3);
  var next = (refPoint , (direction), 1); 
  var prevPattern = this._convertToPattern(prev);
  var nextPattern = this._convertToPattern(next);
 if(prevPattern == 'III' && nextPattern == '0') return 150;
 else return 0;  
 }
}
var Role = function(board)
{
 var computers = [];
 var self = this;
 var isVictory = false;
  = function()
 {
  return isVictory;
 }
 var getScore = function(point)
 {
  var score = 0;
  for(var i = 0 , computer; computer = computers[i++];)
  {
   (point);
   score += ();
  }
  var result = {score: score , point : point};
  return result;
 }
 var getScoreList = function()
 {
  var result = [];
  var avaiablePoints = ();
  for(var i = 0 , point; point = avaiablePoints[i++];) 
  {
   (getScore(point));
  }
  return result;
 }
  = function()
 {
  throw new Error('Unimplemented');
 }
  = function()
 {
  var scoreInfo = getScoreList();
  (function(a,b){
   return  -  ;
  });
  return scoreInfo[0];
 } 
  = function(chesslist)
 {
  var pattern = '';
  if(!chesslist) return '';
  for(var i = 0 ; i <  ; i ++)
  {
   var chess = chesslist[i];
   if(chess == undefined) pattern += '0';
   else if(() == ()) pattern += 'I';
   else pattern += 'Y';
  }
  return pattern ;
 }
  = function(direction)
 {
  switch(direction)
  {
   case LEFT : return RIGHT;
   case RIGHT : return LEFT;
   case TOP : return BOTTOM;
   case BOTTOM : return TOP;
   case LEFT_TOP : return RIGHT_BOTTOM;
   case RIGHT_BOTTOM : return LEFT_TOP;
   case RIGHT_TOP : return LEFT_BOTTOM;
   case LEFT_BOTTOM : return RIGHT_TOP;
   default : alert('Wrong direction');
  }
 } 
 this._checkGameOver = function(point)
 {
  var leftRight = findVictory(point , LEFT);
  var topBottom = findVictory(point , TOP);
  var leftTopRightBottom = findVictory(point , LEFT_TOP);
  var rightTopLeftBottom = findVictory(point , RIGHT_TOP);
  var array = [leftRight , topBottom , leftTopRightBottom , rightTopLeftBottom];
  var victory = [];
  for(var i = 0 ; i <  ; i ++)
  {
   if(array[i].length >= 5) (array[i]);
  }
  if( > 0)
  {
   ();
   (victory);
   isVictory = true;
  }
  if(().length ==0) ();
 }
 var isLicitPoint = function(point)
 {
  return  >= 0 &&  >= 0 &&  <= TRANSVERSE &&  <= VERTICAL 
   && (point) && (point).getOwner() == ()
 }
 var findVictory = function(refPoint , direction)
 {
  var reverse = (direction);
  var result = [];
  var nextPoint ;
  var currPoint = {x:  , y: };
  while(true)
  {
   nextPoint = (currPoint, direction);
   if(!isLicitPoint(nextPoint)) break;
   currPoint = {x : , y:};
  }
  while(true)
  {
  (currPoint);   
   nextPoint = (currPoint , reverse);
   if(!isLicitPoint(nextPoint)) break;  
   currPoint = { x:  , y:  };
  }
  return result;
 }
  = function(point , direction , deep)
 {
  var refPoint = {x:  , y : };
  var result = new Array;
   var index = 1;
   var nextPoint;
   while(index <= deep)
   {
   nextPoint = (refPoint, direction);
    if( < 0 ||  < 0 || 
     > TRANSVERSE ||  > VERTICAL) return null;
    var chess = (nextPoint);
    if(chess)  = {x: , y:};
    (chess);
    refPoint = nextPoint;
    index ++;
   }
   return result;
 } 
 var initialize = function()
 {
  (new Five(self));
  (new Four_Live(self));
  (new Tree_Live(self));
  (new Four_Live1(self));
  (new Tree_Live1(self));
  (new Two_Live(self));
  (new One_Live(self));
  (new Four_End(self));
 }
 initialize();
}
var Machine = function(board, rival)
{
 (this, board);
  = function()
 {
  if(()) return;
  var myPeak = ();
  var rivalPeak = ();
  var peak ;
  if( >= ) peak = myPeak;
  else peak = rivalPeak;
  var chess = new Chess();
  (());
  (chess, );
  this._checkGameOver();
 }
  = function(){return 'machine';}
}
var Person = function(board , rival)
{
 (this, board);
  = function(x,y)
 {
  if(()) return;
  var point = new Object;
   = x;
   = y;
  var chess = new Chess()
  (());
  (chess, point);
  this._checkGameOver(point);
 }
  = function(){ return 'person'; }
}
var UIBase = function()
{
 var self = this;
 this._id = '$UI' + (++ );
 this._globalKey = "";
  = function()
 {
  return "";
 }
 var setGlobalKey = function()
 {
  var magic = '$UI_Items';
  self._globalKey = 'window.'+magic+'.'+self._id;  
  window[magic] = window[magic] || {};
  window[magic][self._id] = self;
 }
 var formatHTML = function(html)
 {
  html = (/\$\$/g, self._globalKey);
  html = (/&&/g,self._id);
  return html;
 } 
 var initUIBase = function()
 {
  setGlobalKey();
 }
  = function()
 {
  return formatHTML(());
 }
  = function()
 {
 var dom = (this._id)
  return dom;
 }
 initUIBase();
}
 = 0;
var ChessUI = function(board, placeholder)
{
 (this);
  = function(){}
  = function()
 {
  var html = '';
  var map = ();
  for(var key in map)
  {
   var onclick = '';
   var className = map[key];
   if(className == '') onclick='$$._setChess('+ key +')';
  html += '<div onclick="'+ onclick +'" class="'+ className +'"></div>';
  }
  return html;
 }
  = function()
 {
  var html = ();
  (placeholder).innerHTML = html;
 }
 this._setChess = function(x,y)
 {
  (x,y);
 }
 ();
}
function getMSIEVersion()
{
 var regex = /MSIE([^;]+)/;
 var userAgent = ;
 var result = (userAgent);
 if(result) return parseInt(result[1]);
}
function initGame()
{
 var version = getMSIEVersion();
 if(version && version <= 8)
 {
  alert('Please use nonIEBrowser(ie9、10except)Playing a game(google chrome 、firefoxwait )');
  return;
 }
 var board = new Board();
 var person = new Person(board);
 var machine = new Machine(board, person);
 var chessUI = new ChessUI(board, 'board');
  = function(x,y)
 {
  (x,y);
  ();
  ();
  if(())
  {
   if(()) alert('You've won');
   else if(()) alert('The machine wins');
   else alert('The game ends, the winner is not decided');
  }
 }
 if((() * 10) % 2)
 {
  alert('Machine');
  ();
  ();
 }
 else
 {
  alert('You are playing the chess');
 }
}
</script>
</body>
</html>

I hope this article will be helpful to everyone's jQuery programming.