| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
|* |
I need to print something like the above ASCII plot for a Gambler's Ruin problem. Where the stake & goal are taken as args. The left most | represents 0 dollars, the right most | represents the goal and the * represents the cash on hand. My program is below:
public class RuinPath {
public static void main(String[] args) {
// TODO - Your solution
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
{
int i = 0;
if (i == 0) {
System.out.print("|");
i++;
while (i < stake) {
System.out.print(" ");
i++;
if (i == stake) {
System.out.print("*");
i++;
while (i > stake && i < goal) {
System.out.print(" ");
i++;
if (i == goal) {
System.out.print("|");
i = 0;
System.out.println();
{
if (Math.random() < 0.5) {
stake++; // win $1
} else {
stake--; // lose $1
}
if (stake == 1 || stake == goal - 1);
break;
}
}
}
}
}
}
}
}
}
what this program prints though is:
| * |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
*
Why does my program not loop so that i can get the left most | to appear to represent 0 dollars all the way through? I have it so i = 0; at the end of the loop so that when it goes back around it should re-loop until the stake is 1 or less than the goal. Instead it re-loops from the middle of the program.
Your logic is a little too complicated. Also, your indentation will make any sort of debugging extremely difficult.
Just take it one step at a time:
public class RuinPath {
public static void main(String[] args) {
int stake = Integer.parseInt(args[0]); // Starting bankroll
int goal = Integer.parseInt(args[1]); // Desired bankroll
while (stake > 0 && stake < goal) {
System.out.print("|");
// Print out the spaces. Using a for loop and
// printing a "*" if the counter variable
// is equal to stake is good way to do it.
// Flip a coin and increment/decrement the stake
System.out.print("|\n");
}
}
}
Here is a broken out solution that might be easier to reason about:
import java.io.PrintStream;
public class SO {
private static int gambleWithCaution(int stake) {
if (Math.random() < 0.5) {
return stake+1; // win $1
} else {
return stake-1; // lose $1
}
}
private static void renderStanding(int stake, int goal) {
System.out.print('|');
for(int dollar = 1; dollar< goal; dollar++) {
if(dollar == stake) {
System.out.print('*');
} else {
System.out.print(' ');
}
}
System.out.println('|');
}
public static void main(String ... args) {
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
while(stake > 0 && stake < goal) {
renderStanding(stake, goal);
stake = gambleWithCaution(stake);
}
System.out.println((stake > goal) ? "You Won!" : "You Lost");
}
}
With the values 3 and 5 you get this output:
| * |
| * |
|* |
| * |
| * |
| *|
| * |
| *|
You Won!
Now that this is seperated out you can have some fun with it like creating a gamble function like this:
private static int gambleWithReclessAbandon(int stake, int goal, double abandon) {
int onTheLine = (int)(Math.random() * (int)(stake * abandon));
if(stake < (0.5)*goal) {
//If you are at less than 50% of your goal then just put it all on the line
//and let it ride :)
onTheLine = stake;
}
if (Math.random() < 0.5) {
return stake+onTheLine; // win $
} else {
return stake-onTheLine; // lose $
}
}
Which can be invoked like this:
//Gamble up to 80% of your current stake
stake = gambleWithReclessAbandon(stake, goal, 0.80);
With the same two values this is what I saw on my first pass (I was sooo close :)):
| * |
| * |
| *|
| * |
| *|
You Lost
Related
I was trying to get a grasp of MiniMax algorithm, and have read up on it. My initial approach was to implement a simple MiniMax algorithm, and then to add alpha-beta pruning. However this is my current code:
public int miniMax(char[] node, int playerNum)
{
int victor = checkWin(node); // returns 0 if game is ongoing, 1 for p1, 2 for p2, 3 for tie.
if(victor != 0) //game over .
return score(victor);
if(playerNum == 2) //AI
{
int bestVal = Integer.MIN_VALUE;
int bestSpot = 0;
for(int i = 0; i < node.length; i++)
{
if(node[i] != '-')
continue;
node[i] = getSymbol(playerNum);
int value = miniMax(node, 1);
if(value > bestVal)
{
bestVal = value;
bestSpot = i;
}
node[i] = '-';
}
return bestSpot;
}
else
{
int bestVal = Integer.MAX_VALUE;
int bestSpot = 0;
for(int i = 0; i < node.length; i++)
{
if(node[i] != '-')
continue;
node[i] = getSymbol(playerNum);
int value = miniMax(node, 2);
if(value < bestVal)
{
bestVal = value;
bestSpot = i;
}
node[i] = '-';
}
return bestSpot;
}
}
And my score function
private int Score(int gameState)
{
if(gameState ==2) //O wins.
return 10;
else if(gameState==1) //X wins
return -10;
return 0;
}
Now, I have a working AI that tries to block my move and win, however sometimes it is making non-intelligent choices for instance this is the output I get if my input read from console is 6,7,8 in that order. It does not attempt to block my win. But in other cases it does.
| O | O | |
| | | |
| X | X | X |
In my second attempt I tried 4,3 and it blocked my winning move.
| | O | |
| X | X | O |
| | | |
I was wondering anyone could point out what is wrong with my implementation?
The behavior of the code for the shown examples is correct!
So why is the threat in the following position not blocked? Why does the program play move 1 instead of 6?
O . . O 1 2
. . . numbering available moves: 3 4 5
X X . X X 6
It is because if the game is lost on perfect play the program just plays the first available move.
The algorithm only cares about win or loss and not in how many moves.
See what happens if the threat is blocked:
O . . O . .
. . . . X . and X wins on his next move
X X O X X O
Ok, so I recently tried to create a java program in eclipse that basically takes an infinite amount of numbers that are 1-100, and stores them in an arraylist. Once this is done, it is supposed to print them out in a horizontal bar graph.
import java.util.ArrayList;
import java.util.Scanner;
/*
* 1-10
* 11-20
* 21-30
* 31-40
* 41-50
* 51-60
* 61-70
* 71-80
* 81-90
* 91-100
*
* Created by Peter browning, PBdeveloping, 2015
*/
public class Asterisks {
public static ArrayList<Integer> array = new ArrayList<Integer>();
public static Scanner reader = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Enter some numbers, 1-100, 0 to stop.");
int input = reader.nextInt();
while (input != 0)
{
if ((input >= 1) && (input <= 100))
{
array.add(input);
input = reader.nextInt();
}
else
{
System.out.println("Error: Number must be 1-100");
input = reader.nextInt();
}
}
for (int i = 1; i <= 10; i++)
{
int firstNumber = ((i - 1) * 10) + 1;
int secondNumber = (i * 10);
int count = 0;
System.out.println(firstNumber + " - " + secondNumber + " |" );
for (int nInsideArray : array)
{
if ((nInsideArray >= firstNumber) && (nInsideArray <= secondNumber))
{
count++;
}
}
for (int x = 0; x < count; x++)
System.out.print("*");
System.out.println();
}
}
}
So the problem that I am having is, whenever I keep on trying to run this program, (lets use the user input "1", "21", then "0" as an example, it should print out:
1-10 | *
11-20 |
21-30 | *
31-40 |
41-50 |
51-60 |
61-70 |
71-80 |
81-90 |
91-100 |
but instead it prints out....
1 - 10 |
*
11 - 20 |
21 - 30 |
*
31 - 40 |
41 - 50 |
51 - 60 |
61 - 70 |
71 - 80 |
81 - 90 |
91 - 100 |
Note: the asterisks are not beside the |'s for some reason, any help would be appreciated. Also if you have a more efficient way to code something like this, I would love to hear your input. I usually learn alot on this website, so please help me with my issue, thanks!
This is because you are printing the pipe with a println(). Change that to print(). When you are done printing asterisks do a println for a new line.
Hey guys if you run this code with the given input you will get a vertical ruler
i'm trying to get a horizontal ruler using the given recursive functions any idea how to get there or hints ???
public class Ruler {
// draw a tick with no label
public static void drawOneTick(int tickLength) {
drawOneTick(tickLength, -1);
}
// draw one tick
public static void drawOneTick(int tickLength, int tickLabel) {
for (int i = 0; i < tickLength; i++)
System.out.print("-");
if (tickLabel >= 0)
System.out.print(" " + tickLabel);
System.out.print("\n");
}
public static void drawTicks(int tickLength) {
if (tickLength > 0) {
drawTicks(tickLength-1);
drawOneTick(tickLength);
drawTicks(tickLength-1);
}
}
public static void drawRuler(int nInches, int majorLength) {
drawOneTick(majorLength, 0);
for (int i = 1; i <= nInches; i++) {
drawTicks(majorLength-1);
drawOneTick(majorLength, i);
}
}
public static void main(String[] args) {
drawRuler(3,4);
}
}
Assuming u want to do Ruler like this:
0 1 2 3 4 5
| | | | | |
| | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
It will be hard to it via recursive methods, because you've limited with print/println(), and because text is 1D ^^
So you wont be able to draw whole "tickline" in 1 method, no, tickline with height N will take N+1 printlines.
But, as you see, i've achieved this ruler, only with loops:
SPOLER!
package com.company;
public class Main {
private static String drawLabels(int count, int offset) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= count; i++) {
sb.append(i);
for (int spaces = 0; spaces < offset; ++spaces) {
sb.append(' ');
}
}
return sb.toString();
}
private static String drawTicks(int count, int offset) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= count; i++) {
sb.append('|');
for (int spaces = 0; spaces < offset; ++spaces) {
sb.append(' ');
}
}
return sb.toString();
}
public static void drawRuler(int nInches, int majorLength) {
// labels part
int offset = (int) Math.pow(2, majorLength - 1) - 1;
System.out.println(drawLabels(nInches, offset));
// rest
for (int line = majorLength; line > 0; --line) {
int ticksOffset = (int) Math.pow(2, line - 1) - 1;
int ticksNumber = nInches * (int) Math.pow(2, majorLength - line);
System.out.println(drawTicks(ticksNumber, ticksOffset));
}
}
public static void main(String[] args) {
drawRuler(5,5);
}
}
Here is a solution more inline with your example. You can still do it with recursion without having to use the power function to calculate the position of the tick.
I suggest to start with a smaller problem: How to draw the minor ticks between the major ticks. We can use the recursion for this one. We also have to realize that we can not anymore print the entire tick at once (as Fen1kz correctly mentioned) because it spans across multiple lines. We will have to loop over the number of lines and in the tick function either draw a tick or an empty space based on which line we are drawing. Once we have this we can easily draw one set of minor ticks. After that it is not too hard to add the rest of the code to repeat the previous and add the major ticks.
For completeness here is the whole code:
public class Ruler {
private static void drawMinorTicks(int line, int ticks) {
if (ticks > 1) {
drawMinorTicks(line, ticks - 1);
}
if (line <= ticks) {
System.out.print('|');
} else {
System.out.print(' ');
}
if (ticks > 1) {
drawMinorTicks(line, ticks - 1);
}
}
private static void drawSingleMajorTick(int line, int ticks, int label) {
if (line <= ticks) {
System.out.print('|');
} else {
System.out.print(label);
}
}
private static void drawMajorTicks(int inches, int line, int ticks) {
drawSingleMajorTick(line, ticks, 0);
for (int i = 1; i <= inches; i++) {
drawMinorTicks(line, ticks - 1);
drawSingleMajorTick(line, ticks, i);
}
}
private static void drawRuler(int inches, int ticks) {
for (int i = 1; i <= ticks + 1; ++i) {
drawMajorTicks(inches, i, ticks);
System.out.println();
}
}
public static void main(String[] args) {
drawRuler(5, 5);
}
}
And the output is:
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | |
| | | | | |
0 1 2 3 4 5
I'm currently trying to create a program that will spit out a chessboard like this one (it looks better in the actual program, just to editor doesn't like me using the "-" symbol so I put them in quotation marks):
-----------------
| | | | |K| | | |
-----------------
| |P| | | |P| | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | |N| | |
-----------------
| | | | |K| | | |
-----------------
I'm using two methods, a showBoard method and an addPiece method. I'm currently stuck with the addPiece method, and I'm trying to make it so the method takes three inputs: the row int, the column int, and the string name (just K for king, for example). However, I can't get the addPiece method to put the pieces where I want them to go, or even at all. Here's what I have so far:
public class ChessBoard {
public static String[][] board = new String[8][8];
public static int row = 0;
public static int col = 0;
public static void addPiece(int x, int y, String r){
board[x][y] = new String(r);
}
public static void showBoard(){
for (row = 0; row < board.length; row++)
{
System.out.println("");
System.out.println("---------------");
for(col = 0; col < board[row].length; col++)
{
System.out.print("| ");
}
}
System.out.println("");
System.out.println("---------------");
}
public static void main(String[] args) {
System.out.println(board.length);
showBoard();
addPiece(1,2,"R");
}
}
I know it has something to do with the way I wrote my addpiece method, but I'm still kind of confused as to how writing the method should be, and that is my best attempt (which doesn't work). Does anyone have any suggestions? Thanks!
You never print the pieces values
for(col = 0; col < board[row].length; col++)
{
if ( board[row][col] != null ) {
System.out.print("|" + board[row][col]);
}
else
System.out.print("| ");
}
And also you'll need to add the pience before you show the board:
addPiece(1,2,"R"); //before
showBoard();
Why are you using new String(r)? Your board array is already an array of Strings, just use:
board[x][y] = r;
Also you are adding the piece after the showBoard method in main, switch them around
addPiece(1,2,"R");
showBoard();
Note that addPiece is changing the state of the board. If you want to see that change, you need to redisplay the new board state.
public class ChessBoard {
public static String[][] board = new String[8][8];
public static void addPiece(int x, int y, String r){
board[x][y] = r;//no need for new String(), board is already made of Strings.
}
public static void showBoard(){
//it's generally better practice to initialize loop counters in the loop themselves
for (int row = 0; row < board.length; row++)
{
System.out.println("");
System.out.println("---------------");
for(int col = 0; col < board[row].length; col++)
{
System.out.print("|"); //you're only printing spaces in the spots
if(board[column][row] == null){
System.ot.print(" ");
}else{
System.out.print(board[column][row]);
}
}
}
System.out.println("");
System.out.println("---------------");
}
public static void main(String[] args) {
System.out.println(board.length);
showBoard(); //board does not have R in it yet.
addPiece(1,2,"R"); //board now has R in it.
showBoard(); //display the new board with R in it.
}
}
The task is to concat the binary of 2 given numbers.
Example:
Given 5 (101) and 3 (011), the result is 46 (concat(101, 011) = 101011)
The code thus far:
public class Concat {
public static void main(String[] args) {
int t = 0;
int k = 5;
int x = 3;
int i = 0;
while (i < 3) {
t = x % 2;
x /= 2;
k <<= 1;
k |= t;
++i;
}
System.out.println(k);
}
}
But the problem is that the above code gives 101110, not 101011.
What's the problem?
Your problem is that you're feeding the bits of the second number in backwards. That's because x%2 is the low order bit:
+---+---+---+ <110
| 1 | 0 | 1 | <-----------------+^
+---+---+---+ |1
+---+---+---+ |1
| 0 | 1 | 1 | ----+0
+---+---+---+ 011>
Cringe at my awesome artistic abilities :-) However, if you already know that it's 3 bits wide, just use:
public class concat {
public static void main (String[] args) {
int t = 0;
int k = 5;
int x = 3;
k <<= 3;
k |= x;
// or, in one line: k = (k << 3) | x;
System.out.println(k);
}
}
In terms of how that looks graphically:
+---+---+---+
k:| 1 | 0 | 1 |
+---+---+---+
+---+---+---+
x:| 0 | 1 | 1 |
+---+---+---+
+---+---+---+---+---+---+
k<<=3:| 1 | 0 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+---+
+---+---+---+
x:| 0 | 1 | 1 |
+---+---+---+
+---+---+---+---+---+---+
k|=3:| 1 | 0 | 1 | 0 | 1 | 1 |
+---+---+---+---+---+---+
^ ^ ^
+---+---+---+
x:| 0 | 1 | 1 |
+---+---+---+
There's no apparent reason for doing it one bit at a time.
You just shift one number and then or with the other number:
int a = 5;
int b = 3;
int c = (a << 3) | b;
I don't know what language you are using, it's almost Java, so I am going with that.
This returns the result you are asking for, though you haven't given rules for determining how you know that 3 should be 011 instead of 11.
I have made the assumption that you want to assume that both numbers have the same number of bits, so 3 is 011 because 5 requires 3 bits.
public class Concat {
public static void main(String[] args) {
System.out.println( joinNums(3,5) );
}
public static int numBits( int n ) {
return (int)Math.ceil( Math.log(n) / Math.log(2) );
}
public static int joinNums( int num1 , int num2 ) {
int bits = Math.max( numBits(num1) , numBits(num2) );
return (num1 << bits) + num2;
}
}