shardeum/community-missions

Code for mission#5

Closed this issue · 3 comments

pragma solidity ^0.6.0;

// Connect4 game smart contract

struct PlayerState {
bytes32[6][7] boardState; // 6 rows, 7 columns
uint playerWins;
uint computerWins;
uint tiedGames;
}

mapping(address => PlayerState) public playerStates;

// The player goes first as red
enum Piece { Red, Yellow }

// Initialize a new game
function newGame() public {
playerStates[msg.sender].boardState = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
];
}

// Place a piece on the board
function placePiece(uint row, uint column) public {
require(playerStates[msg.sender].boardState[row][column] == 0, "This position is already taken");

playerStates[msg.sender].boardState[row][column] = Piece.Red;

// Use the minimax algorithm to determine where to place the computer's piece
// ...

// Check if the game is over
if (gameOver()) {
    // Update the game stats
    if (playerWins()) {
        playerStates[msg.sender].playerWins++;
    } else if (computerWins()) {
        playerStates[msg.sender].computerWins++;
    } else {
        playerStates[msg.sender].tiedGames++;
    }
    newGame();
}

}

// Check if the game is over
function gameOver() private view returns (bool) {
// Check if the player or the computer has won
// ...

// Check if there are any valid moves left
for (uint row = 0; row < 6; row++) {
    for (uint column = 0; column < 7; column++) {
        if (playerStates[msg.sender].boardState[row][column] == 0) {
            return false;
        }
    }
}
return true;

}

// Check if the player has won
function playerWins() private view returns (bool) {
// ...
}

// Check if the computer has won
function computerWins() private view returns (bool) {
// ...
}

frontend part

<title>Connect4</title>

Connect4

<!-- Connect Metamask wallet button -->
<button id="connect-wallet-button">Connect Metamask</button>

<!-- Connect4 board -->
<table id="board">
  <tr>
    <td id="0-0"></td>
    <td id="0-1"></td>
    <td id="0-2"></td>
    <td id="0-3"></td>
    <td id="0-4"></td>
    <td id="0-5"></td>
    <td id="0-6"></td>
  </tr>
  <tr>
    <td id="1-0"></td>
    <td id="1-1"></td>
    <td id="1-2"></td>
    <td id="1-3"></td>
    <td id="1-4"></td>
    <td id="1-5"></td>
    <td id="1-6"></td>
  </tr>
  <tr>
    <td id="2-0"></td>
    <td id="2-1"></td>
    <td id="2-2"></td>
    <td id="2-3"></td>
    <td id="2-4"></td>
    <td id="2-5"></td>
    <td id="2-6"></td>
  </tr>
  <tr>
    <td id="3-0"></td>
    <td id="3-1"></td>
    <td id="3-2"></td>
    <td id="3-3"></td>
    <td id="3-4"></td>
    <td id="3-5"></td>
    <td id="3-6"></td>
  </tr>
  <tr>
    <td id="4-0"></td>
    <td id="4-1"></td>
    <td id="4-2"></td>
    <td id="4-3"></td>
    <td id="4-4"></td>
    <td id="4-5"></td>
    <td id="4-6"></td>
  </tr>
  <tr>
    <td id="5-0"></td>
    <td id="5-1"></td>
    <td id="5-2"></td>
    <td id="5-3"></td>
    <td id="5-4"></td>
    <td id="5-5"></td>
    <td id="5-6"></td>
  </tr>
</table>

<!-- Game stats -->
<div id="game-stats">
  <p>Player wins: <span id="player-wins"></span></p>
  <p>Computer wins: <span id="computer-wins"></span></p>
  <p>Tied games: <span id="tied-games"></span></p>
</div>

<!-- Load web3 library -->

This is my code.
i didn't understand the how to submit it
so i created the issue for this so u can check my code and please revert back..
thank u

w1729 commented

check the readme file of mission 5

Please open a PR with all submission files inside the mission folder you are submitting to.

ok