Created
January 20, 2019 13:04
-
-
Save madhawav/1d8f7d5a1a1a7da91c08fb4912ac860b to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.16+commit.d7661dd9.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
address delegate; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
proposals.length = _numProposals; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function giveRightToVote(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
} | |
/// Delegate your vote to the voter $(to). | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; // assigns reference | |
if (sender.voted) return; | |
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) | |
to = voters[to].delegate; | |
if (to == msg.sender) return; | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegateTo = voters[to]; | |
if (delegateTo.voted) | |
proposals[delegateTo.vote].voteCount += sender.weight; | |
else | |
delegateTo.weight += sender.weight; | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
} | |
function winningProposal() public view returns (uint8 _winningProposal) { | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.16; | |
contract HelloWorld { | |
uint256 counter = 5; | |
address owner = msg.sender; //set owner as msg.sender | |
function add() public { | |
counter++; | |
} | |
function subtract() public { //decreases counter by 1 | |
counter--; | |
} | |
function getCounter() public constant returns (uint256) { | |
return counter; | |
} | |
function kill() public | |
{ | |
//self-destruct function, | |
if(msg.sender == owner) | |
selfdestruct(owner); | |
} | |
function () public payable { | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.16; | |
/** | |
* A Two Player TicTacToe Game played using a smart contract. | |
* P.S. I am just starting to learn solidity. | |
* Author: madhawav | |
*/ | |
contract TicTacToe | |
{ | |
uint boardWidth; | |
uint[] cells; | |
address master = msg.sender; | |
address player1; | |
address player2; | |
uint nextTurn = 0; | |
/** | |
* Must be called by the initiator of contract. Starts a game between caller and a specified opponent | |
*/ | |
function init(uint size, address opponent) public{ | |
if(msg.sender == master && opponent != master) | |
{ | |
boardWidth = size; | |
cells = new uint[](size*size); | |
player1 = master; | |
player2 = opponent; | |
nextTurn = 1; | |
} | |
} | |
/** | |
* Preview board | |
*/ | |
function getBoard() public constant returns (uint[]) { | |
return cells; | |
} | |
/** | |
* View players in game | |
*/ | |
function getPlayers() public constant returns (address,address){ | |
return (player1, player2); | |
} | |
function _setCell(uint x, uint y, uint val) internal { | |
cells[y*boardWidth + x] = val; | |
} | |
/** | |
* Returns true if my turn | |
*/ | |
function checkMyTurn() constant returns (bool){ | |
if(checkResult() != 0) | |
return false; | |
address turnAddress = player1; | |
if(nextTurn == 2) | |
turnAddress = player2; | |
if(msg.sender == turnAddress) | |
return true; | |
return false; | |
} | |
function _passTurn() internal { | |
if(nextTurn == 1) | |
nextTurn = 2; | |
else | |
nextTurn = 1; | |
} | |
function _getCell(uint x, uint y) internal constant returns (uint){ | |
return cells[y*boardWidth + x]; | |
} | |
/** | |
* Play my turn | |
*/ | |
function playCell(uint x, uint y) public { | |
if(checkMyTurn()) | |
{ | |
if(_getCell(x,y) == 0) | |
{ | |
_setCell(x,y,nextTurn); | |
_passTurn(); | |
} | |
} | |
} | |
/** | |
* Check game status | |
* 0: Game on | |
* 1: Player 1 has won | |
* 2: Player 2 has won | |
* 3: Game has tied | |
* 4: Game has not started yet | |
*/ | |
function checkResult() constant returns (uint){ | |
if(nextTurn == 0) | |
return 4; // Game has not started yet | |
if(isWin(1)) | |
return 1; | |
if(isWin(2)) | |
return 2; | |
//Check for blank cells | |
for(uint i = 0; i < boardWidth*boardWidth; i++) | |
{ | |
if(cells[i] == 0) | |
return 0; // Game still on | |
} | |
return 3; //Draw | |
} | |
function isWin(uint player) internal constant returns (bool) { | |
// Check rows | |
for(uint y = 0; y < boardWidth; y++){ | |
bool failed = false; | |
for(uint x = 0; x < boardWidth; x++){ | |
if(_getCell(x,y) != player) | |
{ | |
failed = true; | |
break; | |
} | |
} | |
if(!failed) | |
return true; | |
} | |
//Check columns | |
for(x = 0; x < boardWidth; x++){ | |
failed = false; | |
for(y = 0; y < boardWidth; y++){ | |
if(_getCell(x,y) != player) | |
{ | |
failed = true; | |
break; | |
} | |
} | |
if(!failed) | |
return true; | |
} | |
// Positive Diagonal | |
failed = false; | |
for(x = 0; x < boardWidth; x++) | |
{ | |
if(_getCell(x,x) != player) | |
{ | |
failed = true; | |
break; | |
} | |
} | |
if(!failed) | |
return true; | |
// Negative Diagonal | |
failed = false; | |
for(x = 0; x < boardWidth; x++) | |
{ | |
if(_getCell(boardWidth - x - 1,x) != player) | |
{ | |
failed = true; | |
break; | |
} | |
} | |
if(!failed) | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment