diff --git a/guessing-game-v1/src/controller/GameController.java b/guessing-game-v1/src/controller/GameController.java new file mode 100644 index 0000000..3b52062 --- /dev/null +++ b/guessing-game-v1/src/controller/GameController.java @@ -0,0 +1,72 @@ + +package com.codedifferently.q4.team2.controller; + +import com.codedifferently.q4.team2.model.Difficulty; +import com.codedifferently.q4.team2.service.GameEngine; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@CrossOrigin(origins = "http://localhost:8081") +public class GameController { + + private final GameEngine gameEngine; + + public GameController(GameEngine gameEngine) { + this.gameEngine = gameEngine; + } + + @PostMapping("/start") + public ResponseEntity startGame( + @RequestParam(defaultValue = "EASY") Difficulty difficulty, + @RequestParam(defaultValue = "Anonymous") String playerName + ) { + String gameId = gameEngine.startGame(playerName, difficulty); + return ResponseEntity.ok(gameId); + } + + @PostMapping("/guess") + public ResponseEntity makeGuess( + @RequestBody GuessRequest guessRequest + ) { + + GameEngine.GameSession session = gameEngine.getActiveSession(guessRequest.getGameId()); + + if (session == null) { + return ResponseEntity.badRequest().body("Invalid game session"); + } + + int guess = Integer.parseInt(guessRequest.getText()); + + + boolean isCorrect = gameEngine.validateGuess(guess); + + + String message = prepareGuessResponse(guess, isCorrect, session); + + return ResponseEntity.ok(message); + } + + + private String prepareGuessResponse(int guess, boolean isCorrect, GameEngine.GameSession session) { + if (isCorrect) { + return String.format("Congratulations %s! You guessed the correct number %d in %d attempts!", + session.playerName, guess, session.attempts + 1); + } else { + String hint = (guess < session.secretNumber) ? "higher" : "lower"; + return String.format("Incorrect! Try a %s number. Attempt %d", hint, session.attempts + 1); + } + } + + + public static class GuessRequest { + private String text; + private String gameId; + + + public String getText() { return text; } + public void setText(String text) { this.text = text; } + public String getGameId() { return gameId; } + public void setGameId(String gameId) { this.gameId = gameId; } + } +} \ No newline at end of file diff --git a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java index 94e663f..eda5e7c 100644 --- a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java +++ b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java @@ -3,17 +3,53 @@ import com.codedifferently.q4.team2.model.Difficulty; import com.codedifferently.q4.team2.model.LeaderboardEntry; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; public class GameEngine { protected Scanner console; - protected int secretNumber; + public int secretNumber; boolean isGameOn; - String playerName; - protected int attempts; - Difficulty level = Difficulty.EASY; + public String playerName; + public int attempts; + public Difficulty level = Difficulty.EASY; + + + private Map> leaderboard; + + private Map activeSessions = new ConcurrentHashMap<>(); + + public String startGame(String playerName, Difficulty difficulty) { + // Generate unique game ID + String gameId = UUID.randomUUID().toString(); + + + + // Create a new game session + GameSession session = new GameSession(playerName, difficulty); + activeSessions.put(gameId, session); + + return gameId; + } + + // Inner class to track game state + private class GameSession { + String playerName; + Difficulty difficulty; + int secretNumber; + int attempts; + + public GameSession(String playerName, Difficulty difficulty) { + this.playerName = playerName; + this.difficulty = difficulty; + this.secretNumber = generateNumberToGuess(difficulty); + this.attempts = 0; + } + } + + + - private Map> leaderboard; public GameEngine() { this.console = new Scanner(System.in); @@ -124,7 +160,7 @@ protected int playerGuess(String playerName) { return response; } - boolean validateGuess(int guess) { + public boolean validateGuess(int guess) { return guess == secretNumber; } diff --git a/guessing-game-v2/frontend/src/pages/Play/Play.tsx b/guessing-game-v2/frontend/src/pages/Play/Play.tsx index 3cf679e..c32f8d2 100644 --- a/guessing-game-v2/frontend/src/pages/Play/Play.tsx +++ b/guessing-game-v2/frontend/src/pages/Play/Play.tsx @@ -1,76 +1,84 @@ -import React, {useState} from "react"; +import React, { useState, useEffect } from "react"; import './Play.css'; import numMeCrazyDude from '../../assets/numMeCrazyDude.png'; export const Play: React.FC = () => { - const [inputText, setInputText] = useState(''); - const [response, setResponse] = useState(''); + const [inputText, setInputText] = useState(''); + const [response, setResponse] = useState(''); + const [gameId, setGameId] = useState(''); + const [targetNumber, setTargetNumber] = useState(null); - const handleInputChange = (event:any) => { - setInputText(event.target.value); - }; + useEffect(() => { + const initializeGame = async () => { + try { + const res = await fetch('http://localhost:5137/start', { + method: 'POST' + }); + const data = await res.text(); + setGameId(data); + const targetNum = parseInt(data); // Adjust this based on actual response + if (!isNaN(targetNum)) { + setTargetNumber(targetNum); + } + } catch (error) { + console.error("Error starting game:", error); + } + }; + - const sendGuess = async (guess:string) => { - try { - const res = await fetch(`http://localhost:8081/guess`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(({ text: inputText })), - }); - const data = await res.text(); - setResponse(data); - } catch (error) { - console.error("Error:", error); - } - }; - - return ( - -
-
- -
-

{response}

-
-
- {/* */} - {/* */} - {/*
-

{response}

-
*/} -
-
- {/* */} - - {/* */} - - -
- numMeCrazyBoy -
-
-
- - ); + initializeGame(); + }, []); // Removed nested useEffect, corrected single useEffect + + const handleInputChange = (event: React.ChangeEvent) => { + setInputText(event.target.value); + }; + + const sendGuess = async (guess: string) => { + try { + if(parseInt(guess) === targetNumber) { + setResponse("You Win!!!") + } + const res = await fetch(`http://localhost:8081/guess`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ text: inputText, gameId: gameId }), // Corrected JSON stringify + }); + const data = await res.text(); + setResponse(prevresponse => prevresponse + "\n" + data); + } catch (error) { + console.error("Error:", error); } + }; + + return ( +
{/* Replaced with
*/} +
+ +
+

{response}

+
+
+ + + +
+ numMeCrazyBoy +
+
+
+ ); +} export default Play; \ No newline at end of file