Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class TestMain {
- static Random rnd = new Random();
- public static void main(String[] args) throws InterruptedException {
- TV[][] tvs = new TV[10][10];
- for (int col = 0; col < 10; col++)
- {
- for (int row = 0; row < 10; row++)
- {
- tvs[col][row] = new TV(10, 10, 0);
- }
- }
- for (int i = 0; i < 100; i++)
- {
- resetCursorPos();
- printTV(tvs);
- shuffleTV(tvs);
- Thread.sleep(1000);
- }
- }
- private static void shuffleTV(TV[][] tvs) {
- for (int col = 0; col < 10; col++)
- {
- for (int row = 0; row < 10; row++)
- {
- TV t = tvs[col][row];
- t.powered = (rnd.nextInt(10) % 2 == 0);
- for (int i = rnd.nextInt(100); i > 0; i--)
- {
- if (rnd.nextInt(10) % 2 == 0) {
- t.channelUp();
- }
- else
- {
- t.channelDown();
- }
- }
- }
- }
- }
- private static void resetCursorPos() {
- char escCode = 0x1B;
- int row = 10; int column = 10;
- System.out.print(String.format("%c[%d;%df",escCode,row,column));
- }
- private static void printTV(TV[][] tvs) {
- for (int col = 0; col < 10; col++)
- {
- for (int row = 0; row < 10; row++)
- {
- TV t = tvs[col][row];
- System.out.print(padRight(String.format(" Powered %s : Channel %d ", (t.powered ? "On" : "Off"), t.channel), 28));
- }
- System.out.println();
- }
- }
- public static String padRight(String s, int n) {
- return String.format("%1$-" + n + "s", s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement