pragma solidity ^0.4.24; //import "./erc20.sol"; contract User { /*modifier enoughmoney { require(balanceOf(msg.sender)>=1); _; }*/ address public PlayerA; address public PlayerB; uint8 public endGame = 1; //"1" means game has already ended (default Value is 1) function DefineUserA() public { require(msg.sender != PlayerB); require(endGame == 1); PlayerA = msg.sender; //transferFrom(PlayerA, 0x0, 1); } function DefineUserB() public { //require(msg.sender != PlayerA); require(PlayerA != 0x0000000000000000000000000000000000000000); //playerA should already be defined require(endGame == 1); PlayerB = msg.sender; endGame = 0; //now the game has been startet //transferFrom(PlayerB, 0x0, 1); } //needs a lock, so functions can't be called while game is still going on } contract Fields is User{ //if someone wants to choose a field, we need to make sure that it isn't occupied yet // i describes the row, k the column uint [3][3] public fields; function fieldChange (uint i, uint k, uint value) public { //not sure if I used enum correctly here fields[i-1][k-1] = value; } //this function is called when sb. wants to choose a field } /* k=1 k=2 k=3 ___________ Plan i=1 |___|___|___| i=2 |___|___|___| i=3 |___|___|___| */ contract Winner is Fields{ uint count = 0; function wonGamecolumnk (uint playerValue, uint column) public returns (bool) { //column k for (uint8 i=1; i<=3; i++) { if (fields[i-1][column-1] == playerValue) { count++; } } if (count == 3) { return true; } else { count = 0; } } function wonGamerowi (uint playerValue, uint row) public returns (bool) { //rowi for (uint8 k=1; k<=3; k++) { if (fields[row-1][k-1] == playerValue) { count++; } } if (count == 3) { return true; } else { count = 0; } } function wonGamediagonal (uint8 playerValue) public returns (bool) { //bottom left to top right uint8 i = 3; for (uint8 k=0; k<=2; k++) { i = i - 1; if (fields[i][k] == playerValue) { count++; } } if (count == 3) { return true; } else { count = 0; } //top left to bottom right i = 0; for ( k=0; k<=2; k++) { if (fields[i][k] == playerValue) { count++; } i++; } if (count == 3) { return true; } else { count = 0; } } } contract playGame is Winner { //User user = User(0x2b8a12e2917da6cb482a7055afbd128b6d98291d); uint8 public whichPlayer = 1; uint8 public moveCount = 0; string public youWinner = ""; //provisorisch recognizing Winner event gameOver( address Winner, string message); event Error(string mistake); // is emitted if sb. won the Game mapping (address => uint8) playerSymbol; playerSymbol[PlayerA] = 1; playerSymbol[PlayerB] = 2; function whoseTurn() returns(bytes32[]) { if whichPlayer = 1 { return "PlayerA" } else { return "PlayerB" } } function Play(uint8 i, uint8 k) public { uint8 playerValue = playerSymbol[msg.sender]; if(whichPlayer == playerValue){ if(fields[i-1][k-1] == 0){ fieldChange (i, k, playerSymbol[msg.sender]); if (msg.sender == playerA) { whichPlayer = 2; } else { whichplayer = 1; } moveCount ++; //PlayerB is allowed to play now //checking if PlayerA has won: if (wonGamecolumnk(playerValue, i) == true) { winner(msg.sender); } if (wonGamerowi(playerValue ,k) == true) { winnerA(msg.sender); } if (wonGamediagonal(playerValue) == true) { winnerA(msg.sender); } if (moveCount == 9) { winnerNobody(); } } else { emit Error("field is already occupied") } } else { emit Error("it's not your turn yet") } } //function which is called by PlayerA to choose field i,k /*function PlayB(uint8 i, uint8 k) public { //require(msg.sender == user.PlayerB()); require(whichPlayer == 2); require(fields[i-1][k-1] == 0); fieldChange (i, k, 2); whichPlayer = 1; //PlayerA is allowed to play now moveCount ++; //checking if PlayerA has won: if (wonGamecolumnk(2,i) == true) { winnerB(); } if (wonGamerowi(2,k) == true) { winnerB(); } if (wonGamediagonal(2) == true) { winnerB(); } } //function which is called by PlayerB to choose field i,k */ function winner( address winner) public { //transfer money to PlayerA emit gameOver(winner, "has won the Game"); endGame = 1; //transferFrom(0x0, winner, 2); if (winner == playerA) { youWinner = "playerA"; } else { youWinner = "playerB" } } /*function winnerB() public { //transfer money to PlayerB emit gameOver(PlayerB, "has won the Game"); endGame = 1; youWinner = "PlayerB"; //transferFrom(0x0, PlayerB, 2); }*/ function winnerNobody() public { youWinner = "no winner"; endGame = 1; //transferFrom(0x0, PlayerA, 1); //transferFrom(0x0, PlayerB, 1); } }
0.4.16