-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBattleshipBlockchain.sol
104 lines (92 loc) · 3.21 KB
/
BattleshipBlockchain.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
pragma solidity ^0.5.0;
contract BattleshipBlockchain{
address public p1;
address public p2;
bytes32[] public p1HashLoc;
bytes32[] public p2HashLoc;
uint public pos;
address public setter;
uint public shipsDestroyedp1 = 0;
uint public shipsDestroyedp2 = 0;
bool c;
mapping(address => mapping(uint => bool)) isDestroyed;
bool gameOver = false;
constructor(address _p1, address _p2, bytes32[] memory _p1HashLoc, bytes32[] memory _p2HashLoc) public{
p1 = _p1;
p2 = _p2;
p1HashLoc = _p1HashLoc;
p2HashLoc = _p2HashLoc;
isTurn[_p1] = true;
}
mapping(address => bool) isTurn;
function play(uint _positionToAttack)public{
address defender;
if(msg.sender == p1){
defender = p2;
}else{
defender = p1;
}
require(gameOver == false);
require(msg.sender == p1 || msg.sender == p2, "yo");
require(isDestroyed[defender][_positionToAttack] == false);
require(isTurn[msg.sender] == true);
pos = _positionToAttack;
isTurn[msg.sender] = false;
setter = msg.sender;
}
function reveal(string memory _secret, string memory _ship) public {
require(gameOver == false);
require(msg.sender != setter);
if(msg.sender == p1){
require(checkHash(_secret, _ship) == p1HashLoc[pos], "a1");
if(keccak256(bytes(_ship)) == keccak256(bytes("1"))){
shipsDestroyedp1 += 1;
isTurn[p1] = false;
isTurn[p2] = true;
isDestroyed[p1][pos] = true;
c = true;
if(shipsDestroyedp1 > 2){
gameOver = true;
}
return;
}
c = false;
setter = address(0);
isTurn[p1] = true;
}else{
require(checkHash(_secret, _ship) == p2HashLoc[pos], "a1");
if(keccak256(bytes(_ship)) == keccak256(bytes("1"))){
shipsDestroyedp2 += 1;
isTurn[p2] = false;
isTurn[p1] = true;
isDestroyed[p2][pos] = true;
c = true;
if(shipsDestroyedp2 > 2){
gameOver = true;
}
return;
}
c = false;
setter = address(0);
isTurn[p2] = true;
}
}
//check hacsh correctness
function checkHash(string memory _secret,string memory _ship) public pure returns(bytes32){
return keccak256(bytes(strConcat(_secret,_ship)));
}
//Util functions
function strConcat(string memory _a, string memory _b) internal pure returns (string memory){
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory abcde = new string(_ba.length + _bb.length);
bytes memory babcde = bytes(abcde);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
for (uint i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
return string(babcde);
}
function canPlay() public view returns(bool){
return isTurn[msg.sender];
}
}