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); // is emitted if sb. won the Game function PlayA(uint8 i, uint8 k) public { //require(msg.sender == user.PlayerA()); require(whichPlayer == 1); require(fields[i-1][k-1] == 0); fieldChange (i, k, 1); whichPlayer = 2; moveCount ++; //PlayerB is allowed to play now //checking if PlayerA has won: if (wonGamecolumnk(1,k) == true) { winnerA(); } if (wonGamerowi(1,i) == true) { winnerA(); } if (wonGamediagonal(1) == true) { winnerA(); } if (moveCount == 9) { winnerNobody(); } } //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,k) == true) { winnerB(); } if (wonGamerowi(2,i) == true) { winnerB(); } if (wonGamediagonal(2) == true) { winnerB(); } } //function which is called by PlayerB to choose field i,k function winnerA() public { //transfer money to PlayerA emit gameOver(PlayerA, "has won the Game"); endGame = 1; youWinner = "PlayerA"; //transferFrom(0x0, PlayerA, 2); } 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