TicTac
Описание:Игра в крестики-нолики.



X Wins:0
O Wins:0
Draws:0


Скрипт:

<HTML> <HEAD> <TITLE>Tic-Tac-Toe</TITLE> <STYLE TYPE="text/css"> TD {font-weight:bold} #board TD {width:50px; height:50px; text-align:center; font-size:18pt; cursor:hand} .X {color:blue} .O {color:red} .draw {color:green} </STYLE> <SCRIPT LANGUAGE="JavaScript"> function TicTac() { // Object for tracking the game this.lastMove = true; this.inProcess = true; this.scores = new Object(); this.scores.xScore = 0; this.scores.oScore = 0; this.scores.draws = 0; this.size = 3; this.drawBoard = initBoard; } function buildTable() { // Build the HTML table to be inserted into the document. var tb = "<TABLE BORDER ID=board ONCLICK='doBoardClick();'>"; for (var intRow = 0; intRow < game.size; intRow++) { tb += "<TR>"; for (var intCell = 0; intCell < game.size; intCell++) tb += "<TD>&nbsp;</TD>"; tb += "</TR>"; } tb += "</TABLE>"; return tb; } function initBoard() { document.all.board.outerHTML = buildTable(); game.inProcess = true; game.lastMove == true; } function checkWinner(xCount, oCount) { // Process results of the scan for a winner. if (game.size == xCount) { alert("X Wins!"); game.scores.xScore++; return false; } if (game.size == oCount) { alert("O Wins!"); game.scores.oScore++; return false; } return true; } function checkGame() { // Tests all the directions for a winner. var xCount = 0, oCount = 0, total = 0; var el = document.all.board; // Check horizontal direction. for (var intRows = 0; intRows < el.rows.length; intRows++) { xCount = 0, oCount = 0; for (var intCells = 0; intCells < el.rows[intRows].cells.length; intCells++) { var strCell = el.rows[intRows].cells[intCells]; if ("X" == strCell.innerText) xCount++; if ("O" == strCell.innerText) oCount++; } game.inProcess = checkWinner(xCount, oCount); if (!game.inProcess) return; total += xCount + oCount; } // Check vertical direction. for (var intCells = 0; intCells < el.rows.length; intCells++) { xCount = 0, oCount = 0; for (var intRows = 0; intRows < el.rows[intCells].cells.length; intRows++) { var strCell = el.rows[intRows].cells[intCells]; if ("X" == strCell.innerText) xCount++; if ("O" == strCell.innerText) oCount++; } game.inProcess = checkWinner(xCount, oCount); if (!game.inProcess) return; } // Check diagonal (upper left to lower right). xCount = 0, oCount = 0; for (var intRows = 0; intRows < el.rows.length; intRows++) { var strCell = el.rows[intRows].cells[intRows]; if ("X" == strCell.innerText) xCount++; if ("O" == strCell.innerText) oCount++; } game.inProcess = checkWinner(xCount, oCount); if (!game.inProcess) return; // Check diagonal (lower left to upper right). xCount = 0, oCount = 0; for (var intRows = 0; intRows < el.rows.length; intRows++) { var strCell = el.rows[game.size - intRows - 1].cells[intRows]; if ("X" == strCell.innerText) xCount++; if ("O" == strCell.innerText) oCount++; } game.inProcess = checkWinner(xCount, oCount); if (!game.inProcess) return; if (total == game.size * game.size) { alert("draw"); game.inProcess = false; game.scores.draws++; return } } function updateScore() { // Output new score. for (scores in game.scores) document.all[scores].innerText = game.scores[scores]; } function doBoardClick() { if (game.inProcess) { if ("TD" == event.srcElement.tagName) { var strCell = event.srcElement; // Check whether the cell is available. if ("&nbsp;" == strCell.innerHTML) { strCell.innerText = (game.lastMove ? "X" : "O"); event.srcElement.className = game.lastMove ? "X" : "O"; game.lastMove = !game.lastMove; } } checkGame(); if (!game.inProcess) updateScore(); } } // Manages the game variables var game = new TicTac; </SCRIPT> <SCRIPT FOR="size" EVENT="onclick()" LANGUAGE="JavaScript"> // Shared event handler for the board size radio buttons game.size = parseInt(this.value); game.drawBoard(); </SCRIPT> </HEAD> <BODY> <table border="0" cellpadding="10" cellspacing="0" bgcolor="#222222"><tr><td><h2> <P><INPUT TYPE=BUTTON VALUE="New Game" ONCLICK="game.drawBoard();"> <P><INPUT NAME=size TYPE=RADIO VALUE="3" ID="x3" checked> <LABEL FOR="x3">Игра 3 x 3</LABEL><BR> <INPUT NAME=size TYPE=RADIO VALUE="4" ID="x4"> <LABEL FOR="x4">Игра 4 x 4</LABEL><BR> <INPUT NAME=size TYPE=RADIO VALUE="5" ID="x5"> <LABEL FOR="x5">Игра 5 x 5</LABEL> <P> <table border="0" cellpadding="10" cellspacing="0"><tr><td> <SCRIPT LANGUAGE="JavaScript"> document.write(buildTable()); </SCRIPT> </td></td><td> <TABLE> <TR class=x><TD>X Wins:</TD><TD ID=xScore>0</TD></TR> <TR class=o><TD>O Wins:</TD><TD ID=oScore>0</TD></TR> <TR class=draw><TD>Draws:</TD><TD ID=draws>0</TD></TR> </TABLE> </td></tr></table> </td></tr></table> <!-- copyright (t2) --><div align="center">Сайт создан в системе <a href="http://www.ucoz.ru/" title="Создать сайт бесплатно">uCoz</a><br /></div><!-- /copyright --> </body> </html>

Aport Ranker be number one


Копейка не несет никакой ответственности за предоставленные материалы.
Copyright © 1999 Копейка.

Сайт создан в системе uCoz