I made a little game, but something get wrong - dart

import 'dart:io';
import 'dart:math';
void main() {
String? name;
int numberWish = 0;
int myNumber = 90;
Random random = Random();
print("Hi! Let's play a game! \n What is your name?");
name = stdin.readLineSync();
print("So, $name, write below the number, And If it will be the same number, that I wish, you are the winner and hve the chance to choose gift!");
numberWish = int.parse(stdin.readLineSync()!);
for (var index = 0; index < numberWish; index++) {
myNumber = random.nextInt(numberWish);
print(myNumber);
}
if (myNumber == numberWish) {
print("You are the winner");
} else {
print("Sorry :(");
}
}
It is simple game, where I write the number and if it is the same with randomnumber, I win.
So I run it several times, but I can not win. Random specially choose numbers, which is different from my wished number. I think it is my fault :D. How can I fix it?

Related

How to return two value from a function in dart?

here is my code
import 'dart:io';
import 'dart:math';
void main() {
bool flag = false;
for (int i = 0; i < 100; i++) {
gameCode();
if (userNumber == computerNumber) {
flag = true;
break;
}
}
}
int randomNumber(number) {
Random randNumber = Random();
int random = randNumber.nextInt(number);
return random;
}
gameCode() {
int computerNumber = randomNumber(9);
print("start guessing the number : ");
int userNumber = int.parse(stdin.readLineSync()!);
if (userNumber == computerNumber) {
print("You got it");
}
}
in this code you can see gameCode function. in that function there is two value that i need to use in main function.so how do i return those two keyword from that function ?
//userNumber // computerNumber
this is the variable that i want to return from that code
Dart not support return multiple values in function, you can return it with array, map, or you can use third lib tuple
Dart does not support returning multiple values in the current latest version. I would in your case recommend creating a class for the specific purpose of define the result from gameCode(). So something like this:
class GameCodeResult {
int userNumber;
int computerNumber;
GameCodeResult({
required this.userNumber,
required this.computerNumber,
});
}
Which we can then use like this in your program:
import 'dart:io';
import 'dart:math';
void main() {
bool flag = false;
for (int i = 0; i < 100; i++) {
GameCodeResult result = gameCode();
if (result.userNumber == result.computerNumber) {
flag = true;
break;
}
}
}
final _random = Random();
int randomNumber(int maxNumber) => _random.nextInt(maxNumber);
GameCodeResult gameCode() {
int computerNumber = randomNumber(9);
print("start guessing the number : ");
int userNumber = int.parse(stdin.readLineSync()!);
if (userNumber == computerNumber) {
print("You got it");
}
return GameCodeResult(userNumber: userNumber, computerNumber: computerNumber);
}
Note, I also fixed your randomNumber() method since it is not recommended to generate a new Random() object for each new random number you want. We should instead reuse an instance of Random in our program.
Please refer to below code
import 'dart:io';
import 'dart:math';
void main() {
bool flag = false;
for (int i = 0; i < 100; i++) {
Map<String, dynamic> res = gameCode();
print(res);
if (res['user_number'] == res['computer_number']) {
flag = true;
break;
}
}
}
int randomNumber(number) {
Random randNumber = Random();
int random = randNumber.nextInt(number);
return random;
}
Map<String, dynamic> gameCode() {
int computerNumber = randomNumber(9);
print("start guessing the number : ");
int userNumber =
int.parse(stdin.readLineSync()!);
if (userNumber == computerNumber) {
print("You got it");
}
return {
"computer_number": computerNumber,
"user_number": userNumber,
};
}

dart: access function from list

Edit: i know, always call the first element on list, it isnt the point. i want to call numbers[0] func. and it regenerate new int.actually codes are not same which mine, i have a custom class which based on functions with random int and i need to use list of my custom class , so if i use func in list it will be awesome, how can i make new numbers list each time. when app start list regenerated, but i want when i call the list, it will regenerated
i want to print new int for each print but it prints same int , i tried so many thing and i cant figure out
void main{
int ramdomint(){
final _random = new Random();
int _num = _random.nextInt(100);
return _num;
}
List<int> numbers=[ramdomint(),ramdomint(),ramdomint()];
void printNums(){
for(var i=0;i<3;i++){
List<int> newNumbers =new List.from(numbers); //what can i use for this?
print(newNumbers[0]); //edit:i dont want [i], iwant to use ewNumbers[0] for new int for each time
}
}
printNums();
// expected new int for each but same one
}
solution from a friend:
import 'dart:math';
int get ramdomint => Random().nextInt(100);
List<int> get numbers => [ramdomint, ramdomint, ramdomint];
void main() {
for (var i = 0; i < 3; i++) {
print(numbers[0]);
}
}
Do not nest functions. Move ramdomint and printNums outside main function.
Add an empty list of arguments to the main function.
printNums: pass list of numbers as an argument.
printNums: you don't need to copy the list to the newNumbers if you want only to display the content of the list.
printNums: the problem is, you access only first element of the list (with 0 index).
import 'dart:math';
void main() {
List<int> numbers = [ramdomint(), ramdomint(), ramdomint()];
printNums(numbers);
}
int ramdomint() => Random().nextInt(100);
void printNums(List<int> numbers) {
// Easier way:
for (int item in numbers) {
print(item);
}
// Your way:
for (int i = 0; i < numbers.length; i++) {
print(numbers[i]);
}
}
EDIT:
According to #jamesdlin's comment, you can extend list class to randomize unique values in the list:
import 'dart:math';
void main() {
var numbers = <int>[]..randomize();
printNums(numbers);
}
void printNums(List<int> numbers) {
// Easier way:
for (int item in numbers) {
print(item);
}
// Your way:
for (int i = 0; i < numbers.length; i++) {
print(numbers[i]);
}
}
extension on List<int> {
void randomize({
int length = 3,
int maxValue = 100,
}) {
final generator = Random();
for (var i = 0; i < length; i++) {
add(generator.nextInt(maxValue));
}
}
}
The Problem here is that you are creating a list from the numbers list and accessing only the first element.
So it always prints the first element.
import 'dart:math';
void main() {
int ramdomint(){
final _random = new Random();
int _num = _random.nextInt(100);
return _num;
}
List<int> numbers=[ramdomint(),ramdomint(),ramdomint()];
void printNums(){
for(var i=0;i<3;i++){
print(numbers[i]);
}
}
printNums();
}
Don't want newNumbers, because it is already in List.
and the usage of List.from() - Documentation
Hope that works!

Dart: Accessing function from list

I want to have a new random number every time to print it, but it prints the same on. I tried so many thing, but I can't figure out what's wrong. Help me, please!
import 'dart:math';
int next_int() { return new Random().nextInt(100); }
void main()
{
List<int> list = [next_int(), next_int(), next_int()];
// expected new int each time but got the same one
for (var i = 0; i < 3; i++)
{
List<int> cur_list = new List.from(list);
print(cur_list[0]);
}
}
This code will work as you expect:
import 'dart:math';
int next_int() { return new Random().nextInt(100); }
void main()
{
List<int> list = [next_int(), next_int(), next_int()];
// expected new int each time but got the same one
for (var i = 0; i < 3; i++)
{
List<int> cur_list = new List.from(list);
print(cur_list[i]); // <= Use the index value stored in "i" instead of 0
}
}

Why won't it print out 0? Beginner query

This is a program that prints out all the even numbers between any given integer.
import java.util.*;
public class Question1
{
private int i;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Give me a number!");
int i = scanner.nextInt();
if ((i % 2) != 0)
{
i = i - 1;
do
{
System.out.println(i);
i = i - 2;
} while (i != -2);
}
}
}
So, if I give the number 11, it will print out 10, 8, 6, 4, 2. Why won't it print 0 as well, since my while statement contains i!= -2 and 0 counts as an even number?
Because after scanner.nextInt(); you must put scanner.nextLine(); else, the last element the scanner gets from nextInt(); will be ignored.
Even so, your algorithm is extremely dizzy. why not try:
Scanner in = new Scanner( System.in );
int number = in.nextInt(); in.nextLine();
for( int i = 0; i <= number; i += 2 ) {
System.out.println( i );
}
?

Google Dart Error

import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99 do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard guess = int.parse(temp); //convert String to integer
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
}
while (guess != answer);
print("You got it!");
}
What is wrong?? I deleted the } in
} while (guess != answer);
print("You got it!");
and now in the console it says
Enter your guess:
32
Breaking on exception: object of type NoSuchMethodError
Your guess variable is always null as it's never assigned (it's commented out), try changing your code to:
import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99
do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard
guess = int.parse(temp); //convert String to integer <-- the assignment is what's missing here
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
} while (guess != answer);
print("You got it!");
}
There is a related issue with the readLineSync here which you might want to have a look.
I guess it is because guess is null and null has no >/< operator.
guess is null because you never assign a value.
If guess and answer are not equal this makes a perfect endless loop:
while (guess != answer);
print("You got it!");
}
Wow I failed. Man im tired seems I was typing way to sloppy/fast and left some of my code in the comments :( found it right after seeing this post sigh.. this is the correct working code
import 'dart:math';
import 'dart:io';
void main() {
int guess;
Random rand = new Random(); //create a random number generator
int answer = rand.nextInt(100); //gets a random integer from 0 to 99
do {
print("Enter your guess:");
String temp = stdin.readLineSync(); //read in from the keyboard
guess = int.parse(temp); //convert String to integer
if (guess < answer) {
print("Too low!");
} else if (guess > answer) {
print("Too high!");
}
}
while (guess != answer);
print("You got it!");
}
Thats enough learning of Dart for today. Thanks everyone for the quick replies seems the Dart community is active!
im off to bed! have a great day

Resources