Number reversal game: Difference between revisions

Content added Content deleted
(rearranges in order of the language.)
Line 547: Line 547:
return 1;
return 1;
}</lang>
}</lang>

=={{header|C++}}==
The C code can be used with C++, although the following uses proper C++ iostreams:
<lang CPP>void number_reversal_game()
{
cout << "Number Reversal Game. Type a number to flip the first n numbers.";
cout << "You win by sorting the numbers in ascending order.";
cout << "Anything besides numbers are ignored.\n";
cout << "\t |1__2__3__4__5__6__7__8__9|\n";
int list[9] = {1,2,3,4,5,6,7,8,9};
do
{
shuffle_list(list,9);
} while(check_array(list, 9));

int tries=0;
unsigned int i;
int input;

while(!check_array(list, 9))
{
cout << "Round " << tries << ((tries<10) ? " : " : " : ");
for(i=0;i<9;i++)cout << list[i] << " ";
cout << " Gimme that number:";
while(1)
{
cin >> input;
if(input>1&&input<10)
break;

cout << "\nPlease enter a number between 2 and 9:";
}
tries++;
do_flip(list, 9, input);
}
cout << "Hurray! You solved it in %d moves!\n";
}</lang>

This uses the same helper functions as the C version.

=== Alternate version using the C++ standard library ===
This version uses the C++ standard library (note that none of the C helper functions are needed).
<lang cpp>
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdlib>
#include <ctime>

template<typename T, int size>
bool is_sorted(T (&array)[size])
{
return std::adjacent_find(array, array+size, std::greater<T>())
== array+size;
}

int main()
{
std::srand(std::time(0));

int list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

do
{
std::random_shuffle(list, list+9);
} while (is_sorted(list));

int score = 0;

do
{
std::cout << "Current list: ";
std::copy(list, list+9, std::ostream_iterator<int>(std::cout, " "));

int rev;
while (true)
{
std::cout << "\nDigits to reverse? ";
std::cin >> rev;
if (rev > 1 && rev < 10)
break;
std::cout << "Please enter a value between 2 and 9.";
}

++score;
std::reverse(list, list + rev);
} while (!is_sorted(list));

std::cout << "Congratulations, you sorted the list.\n"
<< "You needed " << score << " reversals." << std::endl;
return 0;
}
</lang>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 763: Line 669:
}
}
}</lang>
}</lang>

=={{header|C++}}==
The C code can be used with C++, although the following uses proper C++ iostreams:
<lang CPP>void number_reversal_game()
{
cout << "Number Reversal Game. Type a number to flip the first n numbers.";
cout << "You win by sorting the numbers in ascending order.";
cout << "Anything besides numbers are ignored.\n";
cout << "\t |1__2__3__4__5__6__7__8__9|\n";
int list[9] = {1,2,3,4,5,6,7,8,9};
do
{
shuffle_list(list,9);
} while(check_array(list, 9));

int tries=0;
unsigned int i;
int input;

while(!check_array(list, 9))
{
cout << "Round " << tries << ((tries<10) ? " : " : " : ");
for(i=0;i<9;i++)cout << list[i] << " ";
cout << " Gimme that number:";
while(1)
{
cin >> input;
if(input>1&&input<10)
break;

cout << "\nPlease enter a number between 2 and 9:";
}
tries++;
do_flip(list, 9, input);
}
cout << "Hurray! You solved it in %d moves!\n";
}</lang>

This uses the same helper functions as the C version.

=== Alternate version using the C++ standard library ===
This version uses the C++ standard library (note that none of the C helper functions are needed).
<lang cpp>
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdlib>
#include <ctime>

template<typename T, int size>
bool is_sorted(T (&array)[size])
{
return std::adjacent_find(array, array+size, std::greater<T>())
== array+size;
}

int main()
{
std::srand(std::time(0));

int list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

do
{
std::random_shuffle(list, list+9);
} while (is_sorted(list));

int score = 0;

do
{
std::cout << "Current list: ";
std::copy(list, list+9, std::ostream_iterator<int>(std::cout, " "));

int rev;
while (true)
{
std::cout << "\nDigits to reverse? ";
std::cin >> rev;
if (rev > 1 && rev < 10)
break;
std::cout << "Please enter a value between 2 and 9.";
}

++score;
std::reverse(list, list + rev);
} while (!is_sorted(list));

std::cout << "Congratulations, you sorted the list.\n"
<< "You needed " << score << " reversals." << std::endl;
return 0;
}
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 845: Line 845:


You took 9 attempts.</pre>
You took 9 attempts.</pre>

=={{header|Eiffel}}==
=={{header|Eiffel}}==
<lang Eiffel>
<lang Eiffel>
Line 986: Line 987:
You needed 9 reversals.
You needed 9 reversals.
</pre>
</pre>

=={{header|Elena}}==
=={{header|Elena}}==
<lang elena>#import system.
<lang elena>#import system.
Line 1,287: Line 1,289:
end program</lang>
end program</lang>

=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<lang go>package main
Line 1,423: Line 1,426:
ENDDO
ENDDO
END</lang>
END</lang>

=={{header|Io}}==
<lang io>withRange := method( a, z,
Range clone setRange(a,z)
)
sorted := withRange(1,9) asList
numbers := sorted clone shuffle
while( numbers==sorted, numbers = numbers shuffle)

steps :=0
stdin := File standardInput
while( numbers != sorted,
writeln(numbers join(" "))
write("Reverse how many? ")
flipcount := stdin readLine asNumber
withRange(0, ((flipcount-1)/2) floor) foreach( i,
numbers swapIndices(i,flipcount-1-i)
)
steps = steps+1
)
writeln("Done! That took you ", steps, " steps")</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,582: Line 1,564:


The new print final score rule is listed instead of the print final score rule in the for printing the player's obituary rules.</lang>
The new print final score rule is listed instead of the print final score rule in the for printing the player's obituary rules.</lang>

=={{header|Io}}==
<lang io>withRange := method( a, z,
Range clone setRange(a,z)
)
sorted := withRange(1,9) asList
numbers := sorted clone shuffle
while( numbers==sorted, numbers = numbers shuffle)

steps :=0
stdin := File standardInput
while( numbers != sorted,
writeln(numbers join(" "))
write("Reverse how many? ")
flipcount := stdin readLine asNumber
withRange(0, ((flipcount-1)/2) floor) foreach( i,
numbers swapIndices(i,flipcount-1-i)
)
steps = steps+1
)
writeln("Done! That took you ", steps, " steps")</lang>


=={{header|J}}==
=={{header|J}}==
Line 1,851: Line 1,854:
print("\nW00t! You scored:", score)
print("\nW00t! You scored:", score)
</lang>
</lang>

=={{header|Mathematica}}==
<lang>Module[{array = Range@9, score = 0},
While[array == Range@9, array = RandomSample@Range@9];
While[array != Range@9,
Print@array; (array[[;; #]] = Reverse@array[[;; #]]) &@
Input["How many digits would you like to reverse?"]; score++];
Print@array; Print["Your score:", score]]</lang>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
Line 1,897: Line 1,908:
Current List: 1 2 3 4 5 6 7 8 9
Current List: 1 2 3 4 5 6 7 8 9
Congratulations! You win! Only 9 reversals.</pre>
Congratulations! You win! Only 9 reversals.</pre>

=={{header|Mathematica}}==
<lang>Module[{array = Range@9, score = 0},
While[array == Range@9, array = RandomSample@Range@9];
While[array != Range@9,
Print@array; (array[[;; #]] = Reverse@array[[;; #]]) &@
Input["How many digits would you like to reverse?"]; score++];
Print@array; Print["Your score:", score]]</lang>


=={{header|Nim}}==
=={{header|Nim}}==
Line 2,791: Line 2,794:
goto [loop]
goto [loop]
end</lang>
end</lang>

=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object NumberReversalGame extends App {
<lang Scala>object NumberReversalGame extends App {
Line 2,967: Line 2,971:
You took 11 attempts to put the digits in order.
You took 11 attempts to put the digits in order.
</pre>
</pre>

=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<lang tuscript>