0

I have tried to make a clicker game with a basic way to tell if the game has been cheated with variables but it just doesnt work right even after intentionally cheating to test it.

Heres my HTML code: (has the js)

var i = 0;

function myFunc() {
  var one = document.getElementById("one");
  if (one.innerHTML == "Click the button to get your first dimension!") {
    one.innerHTML = "You have one dimension."
  } else {
    a = i
    b = a + 1
    i += 1;
    if (i == b) {
      alert("No Cheats")
    } else {
      alert("Cheater!")
    }
    one.innerHTML = "You have " + i + " dimensions."
  }
}
#one {
  width: 200px;
  height: 200px;
  border: 1px solid blue;
}

button {
  width: 200px;
  background: yellow;
  box-shadow: 4px 4px 4px #333;
}
<!DOCTYPE HTML>
<title>Clicker</title>
<noscript>turn on javascript man</noscript>

<head>
</head>
<html>

<body>
  <div id="one">Click the button to get your first dimension!</div>
  <button onclick="myFunc()"> Dimension Creator </button>
</body>

</html>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What did you put into the console to simulate a cheat? The check only happens when the function is called so someone would have to click the `Dimension Creator` button in order for it to be caught – Simeon Oct 14 '22 at 20:12
  • What does it mean to cheat at this? – Barmar Oct 14 '22 at 20:13
  • Which cheat are you applying? Like setting your i higher than it was by merely clicking? – CoderApprentice Oct 14 '22 at 20:14
  • reply to Sinemon: "i = 500" (way more than normal clicking) reply to Barmar: Having more "dimensions" than last time the button was pressed – WinwinTheDumbguy Oct 14 '22 at 20:15
  • reply for CoderApprentice: Yes its setting the i higher than it was by merely clicking – WinwinTheDumbguy Oct 14 '22 at 20:15
  • @WinwinTheDumbguy Well even if I set my i = 500, then in your function, `a = i` straight at the start. Then `b = a + 1 = 501`. Then i += 1 means `i = 501`, which means no cheats in your if-statement ;) – CoderApprentice Oct 14 '22 at 20:18
  • reply for CoderApprentice: oh. i still dont know how to fix the problem so i tried. and now it always alerts "Cheater!" when the buttons clicked even if theres no cheating – WinwinTheDumbguy Oct 14 '22 at 20:20
  • 3
    You can't detect cheating in a game that runs entirely in the client. Whatever variable you compare with, they can update that variable as well. Or they can set a breakpoint in the code and adjust the variables so the condition is OK. – Barmar Oct 14 '22 at 20:20
  • 1
    @WinwinTheDumbguy The way to send a reply to someone is by putting `@` before the name. – Barmar Oct 14 '22 at 20:21
  • 2
    To make a cheat-proof game, the logic has to run on the server, so the user can't change it. – Barmar Oct 14 '22 at 20:22
  • 1
    @WinwinTheDumgguy Yeah this is why websites have a "back-end", data that cannot be manipulated from the outside. The front-end (the client) can be manipulated at will by people, so they can always cheat. – CoderApprentice Oct 14 '22 at 20:22
  • @CoderApprentice ill try and make a back end thanks – WinwinTheDumbguy Oct 14 '22 at 20:25
  • @WinwinTheDumbguy Nowadays you can easily run like an nginx (backend) server with Docker. Then set up a PSQL/MySQL database (also with Docker) and you're good to go. Takes some googling, but good luck :) – CoderApprentice Oct 14 '22 at 20:29
  • @CoderApprentice i use windows so i have to use heroku (and python cause thats the only coding language that i can write code by myself without googling) – WinwinTheDumbguy Oct 14 '22 at 20:37
  • theres a python backend and js frontend related question too and im using that as a base for my backend – WinwinTheDumbguy Oct 14 '22 at 21:11

1 Answers1

0

As comments on your original question pointed out, you need to use a backend to make your game uncheatable. However, a less devoted (and more cheat-prone) approach would be to make the page unusable if the console is opened. You can do this by checking the difference between outer browser dimensions and inner browser dimensions. Like this:

setInterval(function() {
    const widthDiff = window.outerWidth - window.innerWidth > 160;
    const heightDiff = window.outerHeight - window.innerHeight > 160;
    if (widthDiff || heightDiff) {
        console.log('You are cheating.');
        location.reload();
    }
}, 500);

However, this will not work with users who use a floating browser window. You can fix this by using the debugger keyword, but users won't be immediately punished. Like this:

setInterval(function() {
    var minimalUserResponseInMiliseconds = 100;
    var before = new Date().getTime();
    debugger;
    var after = new Date().getTime();
    if (after - before > minimalUserResponseInMiliseconds) { // user had to resume the script manually via opened dev tools 
        console.log("You are cheating");
        location.reload();
    }
}, 500);

(code from this answer)

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • 1
    Your approach seems clever but I have a question: What if the *cheater*'s console is set to be opened in a separate window ? In that case the window height won't change. Am just asking as it seems your approach can easily be bypassed by setting the console (DevTools) to be opened in a separate window. – ThS Oct 14 '22 at 21:00
  • @ths Yes, this could be very easily bypassed and I definitely suggest that OP uses a backend instead. However, this could be part of an approach to prevent plain-old ignorant cheaters. – Michael M. Oct 14 '22 at 21:03
  • The approach is clever and I like it but nothing is perfect I might say. – ThS Oct 14 '22 at 21:05
  • @ths Upon further research, I found a better approach to detect if the console is open. I still doubt that it is uncheatable, but it handles floating console windows. I've edited my answer – Michael M. Oct 14 '22 at 21:11
  • 1
    Yup that's clever and would work for almost anyone but, those who know how to trigger the **inspect element** using the keyboard (`CTRL + SHIFT + C` on Chrome on Windows) could simply bypass the `debugger` call and they can jump to the console tab in the DevTools. I personally use that `inspect element` trick on some websites which use the `debugger` trick. Anyway, that's clever. – ThS Oct 14 '22 at 21:17