pragma solidity ^0.4.24; /* import "github.com/OpenZeppelin/zeppelin-solidity/blob/v1.12.0/contracts/token/ERC20/StandardToken.sol"; contract KantiCoin is StandardToken { string public symbol = "KCOIN"; string public name = "KantiCoin"; uint8 public decimals = 18; function KantiCoin() public { balances[msg.sender] = 1000 * (10 ** uint256(decimals)); totalSupply = 1000 * (10 ** uint256(decimals)); } }*/ contract User { /*modifier enoughmoney { require(balanceOf(msg.sender)>=1); _; }*/ address internal PlayerA; address internal PlayerB; mapping (address => uint8) internal playerSymbol; bool internal endGame = true; // true means game has already ended (default Value is true) function defineUsers() public { require(endGame == true); require(PlayerB == 0x0000000000000000000000000000000000000000); if (PlayerA == 0x0000000000000000000000000000000000000000) { PlayerA = msg.sender; playerSymbol[PlayerA] = 1; } else if (PlayerA != msg.sender) { PlayerB = msg.sender; playerSymbol[PlayerB] = 2; endGame = false; } } //needs a lock, so functions can't be called while game is still going on function getPlayerA() view public returns (address) { return PlayerA; } function getPlayerB() view public returns (address) { return PlayerB; } } 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 uint8 [3][3] internal fields; function fieldChange (uint8 i, uint8 k, uint8 value) internal { //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 function getFieldValue(uint8 i, uint8 k) view public returns (uint8) { return fields[i-1][k-1]; } } /* k=1 k=2 k=3 ___________ Plan i=1 |___|___|___| i=2 |___|___|___| i=3 |___|___|___| */ contract Winner is Fields{ uint count = 0; function wonGameColumnk (uint8 playerValue, uint8 column) internal returns (bool) { //column k count = 0; for (uint8 i=1; i<=3; i++) { if (fields[i-1][column-1] == playerValue) { count++; } } if (count == 3) { return true; } else { return false; } } function wonGameRowi (uint8 playerValue, uint8 row) internal returns (bool) { //rowi count = 0; for (uint8 k=1; k<=3; k++) { if (fields[row-1][k-1] == playerValue) { count++; } } if (count == 3) { return true; } else { return false; } } function wonGameDiagonal (uint8 playerValue) internal returns (bool) { //bottom left to top right count = 0; uint8 i = 3; for (uint8 k=0; k<=2; k++) { i = i - 1; if (fields[i][k] == playerValue) { count++; } } if (count == 3) { return true; } //top left to bottom right count = 0; i = 0; for ( k=0; k<=2; k++) { if (fields[i][k] == playerValue) { count++; } i++; } if (count == 3) { return true; } else { return false; } } } contract playGame is Winner { //User user = User(0x2b8a12e2917da6cb482a7055afbd128b6d98291d); uint8 internal whichPlayer = 1; uint8 internal moveCount = 0; string internal helpWinner = ""; //provisorisch recognizing Winner event gameOver( address Winner, string message ); event Error(string mistake); // is emitted if sb. won the gameOver function whoseTurn() view public returns (string) { if (endGame) { return helpWinner; } else 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, k) == true) { winner(msg.sender); } if (wonGameRowi(playerValue, i) == true) { winner(msg.sender); } if (wonGameDiagonal(playerValue) == true) { winner(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 checkWinner) internal { //transfer money to PlayerA emit gameOver(checkWinner, "has won the Game"); endGame = true; //transferFrom(0x0, myWinner, 2); if (checkWinner == PlayerA) { helpWinner = "PlayerA won"; } else { helpWinner = "PlayerB won"; } PlayerA = 0x0000000000000000000000000000000000000000; PlayerB = 0x0000000000000000000000000000000000000000; } /*function winnerB() public { //transfer money to PlayerB emit gameOver(PlayerB, "has won the Game"); endGame = 1; youWinner = "PlayerB"; //transferFrom(0x0, PlayerB, 2); }*/ function winnerNobody() internal { helpWinner = "no winner!"; endGame = true; //transferFrom(0x0, PlayerA, 1); //transferFrom(0x0, PlayerB, 1); PlayerA = 0x0000000000000000000000000000000000000000; PlayerB = 0x0000000000000000000000000000000000000000; } }
0.4.16