Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tic Tac Toe Smart Contract using Solidity #200

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Level0/E-Commerce/E-Commerce.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Ecommerce {
struct Product {
uint256 id;
string name;
uint256 price;
string deliveryStatus;
}

mapping(uint256 => Product) public products;

event ProductPurchased(uint256 productId, string name, uint256 price);

// Function to allow sellers to register new products
function registerProduct(
uint256 id,
string memory name,
uint256 price
) public {
products[id] = Product(id, name, price, "pending");
}

// Function to allow buyers to purchase a product
function purchaseProduct(uint256 id) public payable {
require(
products[id].deliveryStatus == "pending",
"Product is not available for sale"
);
// Ensure the buyer has sufficient funds
require(msg.value >= products[id].price, "Insufficient funds");

// Update the product's delivery status to "in progress"
products[id].deliveryStatus = "in progress";

// Deduct the purchase price from the buyer's account
msg.sender.transfer(products[id].price);

emit ProductPurchased(id, products[id].name, products[id].price);
}

// Function to allow sellers to mark a product as delivered
function markProductAsDelivered(uint256 id) public {
// Ensure the product's delivery status is "in progress"
require(
products[id].deliveryStatus == "in progress",
"Product has not been purchased or is already delivered"
);

// Update the product's delivery status to "delivered"
products[id].deliveryStatus = "delivered";

// Transfer the purchase price to the seller's account
address seller = seller.transfer(products[id].price); // seller's address here ;
}
}
7 changes: 7 additions & 0 deletions Level0/E-Commerce/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### E-Commerce Smart Contract

- This smart contract has a struct to represent a product, with fields for the product ID, name, price, and delivery status.
- It also has a mapping from product ID to product struct, to store the catalog of available products.
- The contract has functions to allow sellers to register new products, buyers to purchase products, and sellers to mark products as delivered.
- The purchaseProduct function checks that the product is available for sale and that the buyer has sufficient funds before updating the product's delivery status to "in progress" and transferring the purchase price from the buyer's account to the contract's account.
- The markProductAsDelivered function checks that the product's delivery status is "in progress" before updating the delivery status to "delivered" and transferring the purchase price to the seller's account.
32 changes: 32 additions & 0 deletions Level2/Tic-Tac-Toe/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Tic Tac Toe : Smart Contract using Solidity

In this repository we will see a Smart Contract for Tic-Tac-toe : a famous game uing Solidity programming Language.<br><br>

> ### **Tips: Before we Begin**
>
> #### To view your result as expected , Follow this steps :
>
> - Right Click anywhere on the screen.
> - Click on Inspect.
> - Go to Body Element.
> - Change Font-Family to monospace.
> - Change White-Space to pre-wrap.
> <br><br>

<br>

## Task Done: Functions used in this Contract.

- performMove -> Used to make a move.
- currentPlayerAddress -> Returning the address of the current player playing.
- currentPlayerShape -> Returning the shape of the current player playing
- winner -> Used to return the winner of the game.
- isGameOver -> To check whether we have a clear winner.
- winningPlayerShape -> Used to check whick player won the game according to the game rules.
- stateToString -> Used to display the game state at a current time.

## Screenshot of the working contract .

![Screenshot 2022-10-14 230458](https://user-images.githubusercontent.com/94488557/201542570-53a6f961-4b30-4410-bfeb-4b3f7e1d29d2.png)


177 changes: 177 additions & 0 deletions Level2/Tic-Tac-Toe/tic-tac-toe.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
pragma solidity ^0.4.24;

contract TicTacToe {
address player1;
address player2;
uint8 current_move = 0;
enum SquareState {
Empty,
X,
O
}
SquareState[3][3] board;

constructor(address _player2) public {
require(_player2 != 0x0);
player1 = msg.sender;
player2 = _player2;
}

function performMove(uint8 xpos, uint8 ypos) public {
require(msg.sender == player1 || msg.sender == player2);
require(!isGameOver());
require(msg.sender == currentPlayerAddress());

require(positionIsInBounds(xpos, ypos));
require(board[xpos][ypos] == SquareState.Empty);

board[xpos][ypos] = currentPlayerShape();
current_move = current_move + 1;
}

function currentPlayerAddress() public view returns (address) {
if (current_move % 2 == 0) {
return player2;
} else {
return player1;
}
}

function currentPlayerShape() public view returns (SquareState) {
if (current_move % 2 == 0) {
return SquareState.X;
} else {
return SquareState.O;
}
}

function winner() public view returns (address) {
SquareState winning_shape = winningPlayerShape();
if (winning_shape == SquareState.X) {
return player2;
} else if (winning_shape == SquareState.O) {
return player1;
}
return 0x0;
}

function isGameOver() public view returns (bool) {
return (winningPlayerShape() != SquareState.Empty || current_move > 8);
}

function winningPlayerShape() public view returns (SquareState) {
// Columns
if (
board[0][0] != SquareState.Empty &&
board[0][0] == board[0][1] &&
board[0][0] == board[0][2]
) {
return board[0][0];
}
if (
board[1][0] != SquareState.Empty &&
board[1][0] == board[1][1] &&
board[1][0] == board[1][2]
) {
return board[1][0];
}
if (
board[2][0] != SquareState.Empty &&
board[2][0] == board[2][1] &&
board[2][0] == board[2][2]
) {
return board[2][0];
}
// rows
if (
board[0][0] != SquareState.Empty &&
board[0][0] == board[1][0] &&
board[0][0] == board[2][0]
) {
return board[0][0];
}
if (
board[0][1] != SquareState.Empty &&
board[0][1] == board[1][1] &&
board[0][1] == board[2][1]
) {
return board[0][1];
}
if (
board[0][2] != SquareState.Empty &&
board[0][2] == board[1][2] &&
board[0][2] == board[2][2]
) {
return board[0][2];
}
// Diagonals
if (
board[0][0] != SquareState.Empty &&
board[0][0] == board[1][1] &&
board[0][0] == board[2][2]
) {
return board[0][0];
}
if (
board[0][2] != SquareState.Empty &&
board[0][2] == board[1][1] &&
board[0][2] == board[2][0]
) {
return board[0][2];
}
return SquareState.Empty;
}

function stateToString() public view returns (string) {
return
string(
abi.encodePacked(
"\n",
rowToString(0),
"\n",
rowToString(1),
"\n",
rowToString(2),
"\n"
)
);
}

function rowToString(uint8 ypos) public view returns (string) {
return
string(
abi.encodePacked(
squareToString(0, ypos),
"|",
squareToString(1, ypos),
"|",
squareToString(2, ypos)
)
);
}

function squareToString(uint8 xpos, uint8 ypos)
public
view
returns (string)
{
require(positionIsInBounds(xpos, ypos));
if (board[xpos][ypos] == SquareState.Empty) {
return " ";
}
if (board[xpos][ypos] == SquareState.X) {
return "X";
}
if (board[xpos][ypos] == SquareState.O) {
return "O";
}
}

function positionIsInBounds(uint8 xpos, uint8 ypos)
public
pure
returns (bool)
{
return (xpos >= 0 && xpos < 3 && ypos >= 0 && ypos < 3);
}
}