I am trying to make a checkers game for php

two players on the same computer here is what I got and was wondering if there's anything

wrong with this code

note: piece movement would be based from text move A1 to B2

function drawBoard()
{
			global $cBoard;

                	$R = "<img src=rp.gif>"; //Red piece
					$B = "<img src=bp.gif>";     //black piece
					$ES = "<img src=blank.gif>"; // legal square image
					$IS = "<img src=blank1.gif>"; //illegal square image





	$cBoard = array(
			  array($IS, $R, $IS, $R, $IS, $R, $IS, $R),
			  array($R, $IS, $R, $IS, $R, $IS, $R, $IS),
			  array($IS, $R, $IS, $R, $IS, $R, $IS, $R),
			  array($ES, $IS, $ES, $IS, $ES, $IS, $ES, $IS),
			  array($IS, $ES, $IS, $ES, $IS, $ES, $IS, $ES),
		          array($B, $IS, $B, $IS, $B, $IS, $B, $IS),
			  array($IS, $B, $IS, $B, $IS, $B, $IS, $B),
		          array($B, $IS, $B, $IS, $B, $IS, $B, $IS)
	);



            echo "<table border='3' width='200'>\n";
			foreach ($cBoard as $row)
			{
	     		 switch($row)
	     		 {
	     		 	case "8": $row = 0; break;
	     		 	case "7": $row = 1; break;
	     		 	case "6": $row = 2; break;
	     		 	case "5": $row = 3; break;
	     		 	case "4": $row = 4; break;
	     		 	case "3": $row = 5; break;
	     		 	case "2": $row = 6; break;
	     		 	case "1": $row = 7; break;

	     		 }

					echo "<tr>\n";
     				foreach ($row as $piece){
               				echo "<td>";
               				echo "$piece ";
               				echo "</td>\n";
               				 switch($Piece)
				             {
				             	case "A": $piece = 0; break;
				             	case "B": $piece = 1; break;
				             	case "C": $piece = 2; break;
				             	case "D": $piece = 3; break;
				             	case "E": $piece = 4; break;
				             	case "F": $piece = 5; break;
				             	case "G": $piece = 6; break;
				             	case "H": $piece = 7; break;
				     		 }
     				}
			}
					echo "</tr>\n";
					echo "</table>\n";

Dani AI

Generated

already hit the most immediate bug: PHP variable names are case sensitive, so $Piece will never match $piece. Beyond that, the loop logic is fragile: your code is switching on the whole $row (which is an array), then trying to convert row/column labels inside the inner loop. Use the foreach key form or a plain for-loop so you have numeric row/column indexes you can use directly.

Keep game state separate from presentation. Store the logical board as a 2D array of simple values ('r', 'b', 'R', 'B', null) in $_SESSION or a serialised form, and build the HTML table from that when you render the page. That makes move validation and persistence much simpler — see the PHP session docs: PHP Sessions.

A robust way to parse a text move (like the A1 style you mentioned) is to validate the string then convert letter->column and number->row. Example parsing routine:

function parseCoord($s) {
  $s = strtoupper(trim($s));
  if (!preg_match('/^[A-H][1-8]$/', $s)) return false;
  $col = ord($s[0]) - ord('A');   // 0..7
  $row = 8 - intval($s[1]);      // 8->0 .. 1->7
  return [$row, $col];
}

For move rules implement these checks in order: input valid, source contains current player piece, destination empty and on a dark square, diagonal delta is correct (1 for simple moves, 2 for captures), direction allowed for non-king pieces, remove jumped piece on captures, then promote if needed. After a capture, check for additional capture opportunities.

Enable full error reporting and inspect the board with var_dump or print_r while debugging. For iteration details see the foreach docs: foreach. Converting to a click-based UI later will make moves simpler for users, but text parsing plus the checks above will get your turn logic working reliably.

Recommended Answers

All 2 Replies

$piece in your switch block is capitalized, uncapitalized it.

switch($Piece)

I am having trouble making the pieces movie

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.